How to plot the surface of an arbitrary given TABLE (not matrix) with X,Y,Z values?

1 visualización (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Oct. de 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 comentarios
Andrei Bobrov
Andrei Bobrov el 21 de Oct. de 2011
Z = accumarray( [bx(:),by(:)], YourTable(:,3), [],[], NaN );
Walter Roberson
Walter Roberson el 21 de Oct. de 2011
Some day I'll memorize the order of parameters for accumarray...

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 20 de Oct. de 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.
  1 comentario
Emerson De Souza
Emerson De Souza el 21 de Oct. de 2011
Thank you Jan,
your suggestion works as long the length of X and Y are identical which must not be the case for real applications.
For the case that X and Y have different length I used the method suggested by Walter.
But thank you again for your attention
Emerson

Iniciar sesión para comentar.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by