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
0 个评论
采纳的回答
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 个评论
更多回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!