problem with plotting a 3D graph

Hi,
Please I need help to get the following code working:
Blue = [2 3 4 5 6]';
Green = [2 4 6 10 12]';
Red = [3 6 9 12 15]';
BL = Blue(1:end);
GR = Green(1:end);
RD = Red(1:end);
join = [];
for i = 1:GR
for j = 1:RD
join(i+1,j+1) = BL;
end;
end;
surf(join)
When I ran the code in Matlab, I got the following error message: "??? Subscripted assignment dimension mismatch." My intention was to plot the data in 3D.
Thanks
James

 Accepted Answer

Star Strider
Star Strider on 9 Aug 2012
Edited: Star Strider on 9 Aug 2012
If all you want to do is plot Blue as a function of Green and Red, I suggest simply:
scatter3(GR, RD, BL, 'p')
to plot them as five-pointed stars, or:
stem3(GR, RD, BL)
if you want to know where Blue's Green and Red coordinate locations are as well.
If you want to plot join as a surface, you need to be more specific, and calculate join differently. I refer you to meshgrid.
One problem is with the way you define join in your loop. I don't understand what you want to do, but to avoid the error you got, I suggest changing it to something like:
join = [];
for i = 1:max(GR)
for j = 1:max(RD)
join(i+1,j+1,:) = BL;
end
end
although you will have to change your code significantly to plot your data as a surface.

6 Comments

Thanks Star Strider. I actually wanted to plot join as a surface and have tried meshgrid. I did [X Y] = meshgrid(GR, RD); and then surf(X,Y,BL); Then I got an error message"Data dimensions must agree."
Well, you want to create a surface out of 3 [5 x 1] vectors. That's a lot to ask of them!
The best I can come up with is:
Blue = [2 3 4 5 6]';
Green = [2 4 6 10 12]';
Red = [3 6 9 12 15]';
P = [ones(size(Blue)) Green Red]\Blue;
[XG YR] = meshgrid(Green, Red);
ZB = P(1) + P(2)*XG + P(3)*YR;
figure(32767)
surf(XG, YR, ZB)
xlabel('Green')
ylabel('Red')
zlabel('Blue')
grid on
It creates a regression surface and a linear regression of Blue on [Green Red]. It is an exact fit, so it seems an appropriate solution. If there's a known functional relationship between your variables, use that instead.
Okay, I understand things better now. Please explain this line: P = [ones(size(Blue)) Green Red]\Blue;
P are the parameters vector calculated by solving this matrix equation:
[1 G R]*P = B
A bit less cryptically, it's a multiple linear regression (more formally implemented by the regress function in the Statistics Toolbox). The way I interpreted your code, I am assuming that Blue is a linear function of Green and Red (along with a constant term, necessary in such situations, something like the y-intercept in bivariate regression). You don't necessarily have to use the constant term in your situation, so feel free to simply regress Blue against Green and Red without the constant term if you like. The fit may not be as good, however.
MATLAB uses the backslash ‘\’ operator to implement a least-squares solution to such equations. It's quite useful.
Many thanks for your help and explanations, Star Strider. Thanks to you too, Azzi. I certainly have a better understanding of plotting a 3D graph and the useful information that I have now will help me formulate my problem better.
My pleasure!
I learned a lot in the process as well.

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 9 Aug 2012
Edited: Azzi Abdelmalek on 9 Aug 2012
%if you want plot a 3D graph with x axis Blue and Yaxis Green and corresponding data : example : z=rand(5,5)
Blue = [2 3 4 5 6]';
Green = [2 4 6 10 12]';
z=rand(5,5)
surf(Blue,Green,z)

1 Comment

Thank, Azzi. The problem in my case remains that I do not have a matrix in the z position and would just like to plot the 3 vectors.

Sign in to comment.

Kartik Verma
Kartik Verma on 1 Dec 2020
Take two vectors, x and y, of your choice of length 50. Consider a function
z=x^2+y^2-xy
Make a 3D surface plot for x,y and z with proper axis titles, legends etc.

Community Treasure Hunt

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

Start Hunting!