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

 Respuesta aceptada

Star Strider
Star Strider el 9 de Ag. de 2012
Editada: Star Strider el 9 de Ag. de 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 comentarios

James
James el 9 de Ag. de 2012
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."
Star Strider
Star Strider el 9 de Ag. de 2012
Editada: Star Strider el 9 de Ag. de 2012
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.
James
James el 10 de Ag. de 2012
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.
James
James el 12 de Ag. de 2012
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.
Star Strider
Star Strider el 12 de Ag. de 2012
My pleasure!
I learned a lot in the process as well.

Iniciar sesión para comentar.

Más respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 9 de Ag. de 2012
Editada: Azzi Abdelmalek el 9 de Ag. de 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 comentario

James
James el 9 de Ag. de 2012
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.

Iniciar sesión para comentar.

Kartik Verma
Kartik Verma el 1 de Dic. de 2020

0 votos

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.

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 9 de Ag. de 2012

Respondida:

el 1 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by