![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177318/image.jpeg)
How can I plot various vectors together with surf or mesh when they have different x, y and z values?
3 views (last 30 days)
Show older comments
Andreas Theodorakopoulos
on 5 Jun 2015
Answered: Sreeja Banerjee
on 8 Jun 2015
Hello,
assuming I have 4 times of these two vectors
G=[-1 2 5 -3 3 4] %(x)
P=[36 45 23 54 56 23] %(y)
G=[2 -3 5 -3 3 1]
P=[46 23 45 79 23 12]
G=[2 2 -3 2 3 -1]
P=[36 45 23 54 56 23]
G=[-1 -4 -4 -3 3 2]
P=[36 37 14 54 56 56]
each and every pair gives you a plot if you just do plot(G,P)
What I want to do is plot them on a mesh or surf so that X axis will have the P values Y axis will have the G values for each P and the Z axis will just be a number from 1:4 or as many the plots are.
my thoughts up to now come to interpolation in order to fix all the P values somehow , but I really do not know how to do it.
Any suggestions?
/Andreas T.
0 Comments
Accepted Answer
Sreeja Banerjee
on 8 Jun 2015
Hi Andreas,
From your question I understand that you have two 1D vectors G and P. These vectors can have different values at different instances, for example, in the question you have mentioned 4 different pairs of G and P. You want to create a surface plot with these two vector and have the four different values shown on four different levels (z-axis).
Since G and P are only 1D vectors, we cannot create a surface plot which them. However, we can use the function MESHGRID to create a rectangular grid in 2-D. I have shown below a simple exmple on how you can do that:
G=[-1 2 5 -3 3 4];
P=[36 45 23 54 56 23];
[x,y] = meshgrid(G,P);
z = 0*x + 1;
surf(x,y,z,'Marker','*');
hold on;
G=[2 -3 5 -3 3 1];
P=[46 23 45 79 23 12];
[x,y] = meshgrid(G,P);
z = 0*x + 2;
surf(x,y,z,'Marker','*');
hold on;
G=[2 2 -3 2 3 -1];
P=[36 45 23 54 56 23];
[x,y] = meshgrid(G,P);
z = 0*x + 3;
surf(x,y,z,'Marker','*');
hold on;
G=[1 -4 -4 -3 3 2];
P=[36 37 14 54 56 56];
[x,y] = meshgrid(G,P);
z = 0*x + 4;
surf(x,y,z,'Marker','*');
hold off;
The figure obtained is as follows:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177318/image.jpeg)
You can read more about MESHGRID at the following documentation: http://www.mathworks.com/help/matlab/ref/meshgrid.html
0 Comments
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!