How to plot the surface of an arbitrary given TABLE (not matrix) with X,Y,Z values?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I want to plot the surface Z of arbitrary given values for each X,Y-pair.
I usually IMPORT the tables with the X,Y,Z data, so I they are not a matrix.
An example is displayed below:
1 1 0.171121066356432
1 2 0.0326008205305280
1 3 0.561199792709660
2 1 0.881866500451810
2 2 0.669175304534394
2 3 0.190433267179954
3 1 0.368916546063895
3 2 0.460725937260412
3 3 0.981637950970750
I tried the following lines to plot the surface
[X,Y] = meshgrid(1:1:3, 1:1:3);
Z=rand(9,1);
surf(X,Y,Z)
But I get the following error:
??? Error using ==> surf at 78
Z must be a matrix, not a scalar or vector.
Error in ==> Untitled2 at 5
surf(X,Y,Z)
Question: I replaced the x,y coordinate with the function meshgrid but what do I make wih the Z values?
I'm aware that Z is not a matrix, but this is exactly the problem!!!
I hope someone knows how to correct these commands
Thanks,
Emerson
0 Commenti
Risposta accettata
Walter Roberson
il 20 Ott 2011
[ux, ax, bx] = unique(YourTable(:,1));
[uy, ay, by] = unique(YourTable(:,2));
[X, Y] = ndgrid(ux, uy);
Z = accumarray( [bx(:),by(:)], YourTable(:,3), [], NaN );
surf(X,Y,Z);
This code will even average the z entries if there are multiple (x,y) pairs with different z.
If I have done it right, it does not need to be fed non-negative integers in the first two table columns.
I set the code up so that if there is some combination of (x,y) for which there is no table value, then NaN will be inserted as the Z value. surf() understands NaN as meaning not to plot that square. But you could put any other fixed value numeric there instead. In particular, in the special case where you want 0 for missing values, you can abbreviate the accumarray call to
Z = accumarray( [bx(:),by(:)], YourTable(:,3) );
3 Commenti
Più risposte (1)
Jan
il 20 Ott 2011
You can reshape Z such that it becomes a matrix:
[X,Y] = meshgrid(1:1:3, 1:1:3);
Z = reshape(rand(9, 1), 3, 3);
surf(X, Y, Z)
Of course you would use "rand(3,3)" in a real application.
Vedere anche
Categorie
Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!