How to do a surf plot for non - uniform data?
32 views (last 30 days)
Show older comments
Hi all....
I have the matrices x,y and z each of size 30x1. x & y are the X,Y coordinates and z represents the height of the coordinate at that point. I would like do something like a surf plot so that at the respective coordinate i get a surf with the corresponding height)
I have tried something like this.
[x,y] = meshgrid(-3:1/4.9:3); z = p1; //here z is converted from 30x1 to 30x30 because only then peaks can be applied z = peaks(z); surfl(x,y,z); shading interp colormap(jet); axis([-3 3 -3 3 0 2])
Here I have kept the scale fixed as -3 to +3 but the difficulty is I am not able to figure out the 30 coordinates.
My actual x,y, z looks similar to this.
x = [180; 270; 120; 160; 225; 290; 335; 100; 150; 225; 305; 355; 95; 145; 225; 315; 360; 110; 150; 225; 305; 345; 125; 165; 225; 290; 330; 175; 225; 280]; y = [45; 45; 90; 105; 105; 105; 90; 145; 155; 155; 155; 145; 200; 200; 200; 200; 200; 260; 250; 250; 250 ; 260; 320; 310; 310; 310; 320; 360; 360; 360]; z = [1; 5; 6; 19; 18; 16; 16; 4; 16; 19; 23; 20; 6; 18; 21; 21; 13; 12; 12; 20; 21; 20; 11; 8; 23; 18; 17; 19; 20; 1];
And I would like to plot the x&y and then do the surf plot.
Thanks in advance for the help.
0 Comments
Answers (2)
Richard McCulloch
on 27 Jul 2013
Edited: Richard McCulloch
on 27 Jul 2013
I'm not sure that I entirely understand the question or the data, but is this what you want?
x = [180; 270; 120; 160; 225; 290; 335; 100;...
150; 225; 305; 355; 95; 145; 225; 315; ...
360; 110; 150; 225; 305; 345; 125; 165; ...
225; 290; 330; 175; 225; 280];
y = [45; 45; 90; 105; 105; 105; 90; 145; 155;...
155; 155; 145; 200; 200; 200; 200; 200; ...
260; 250; 250; 250 ; 260; 320; 310; 310; ...
310; 320; 360; 360; 360];
temp = [1; 5; 6; 19; 18; 16; 16; 4; 16; 19; 23;...
20; 6; 18; 21; 21; 13; 12; 12; 20; 21; ...
20; 11; 8; 23; 18; 17; 19; 20; 1];
z=zeros(length(x));
for i=1:length(x)
z(i,i)=temp(i);
end
surf(x,y,z)
It creates the following image:
2 Comments
Richard McCulloch
on 27 Jul 2013
Edited: Richard McCulloch
on 27 Jul 2013
That's what I was guessing you wanted. Are you looking for something like this:
z_temp = [1; 5; 6; 19; 18; 16; 16; 4; 16; 19; 23;...
20; 6; 18; 21; 21; 13; 12; 12; 20; 21; ...
20; 11; 8; 23; 18; 17; 19; 20; 1];
x=linspace(-3,3,30);
y=x;
for i=1:length(x)
for j=1:length(y)
z(i,j)=z_temp(i);
end
end
surf(x,y,z)
It might be easier to tell if you describe the function parameters. The image associated with the code above is this
Looking at a scatter plot of the data, I don't see how a clean surface can really be made for the given x,y,z combinations. Maybe I'm just missing something.
Shivam Anand
on 11 May 2022
x=[32 20 67 1 98 34 57 65 24 82 47 55 8 51 13 14 18 30 37 39 10 33 21 26 38 81 83 60 95 22 17 5 72 46 99 52 12 25 96 29 70 85 43 69 19 78 97 31 89 53 2 91 48 71 61 15 36 84 94 50 11 80 6 7 49 74 9 88 40 79 27 68 73 64 63 59 86 23 35 58 45 28 100 42 93 87 16 90 41 66 54 92 77 4 62 76 75 56 3 44];
y=[96 75 24 9 83 49 27 77 3 23 17 31 40 13 7 52 51 21 98 47 64 79 78 91 44 16 15 100 84 99 63 68 70 30 54 76 97 73 33 5 88 8 71 66 62 25 60 42 72 45 18 11 28 59 89 65 10 55 69 81 12 26 20 95 87 41 74 50 93 22 43 90 14 34 82 35 56 38 80 32 1 57 6 36 37 61 29 58 2 48 4 46 67 53 92 86 94 19 39 85];
z=[55 31 11 45 83 36 86 49 15 57 42 46 8 94 88 47 54 81 98 41 32 35 56 85 9 89 37 60 23 62 67 100 78 76 73 80 10 20 68 34 77 93 1 63 53 12 22 99 91 40 84 24 33 3 43 19 92 97 6 82 64 25 26 79 95 4 44 58 5 21 70 29 65 87 96 90 51 14 18 2 72 28 71 39 52 7 27 59 50 61 48 30 66 69 17 13 74 16 75 38];
xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
% Z = griddata(x,y,z,X,Y,'natural');
% Z = griddata(x,y,z,X,Y,'cubic');
Z = griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!