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

조회 수: 3 (최근 30일)
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

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2011년 10월 20일
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
Emerson De Souza
Emerson De Souza 2011년 10월 21일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by