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

1 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 9 日
why
BL = Blue(1:end);
you can writte
BL = Blue

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2012 年 8 月 9 日
編集済み: Star Strider 2012 年 8 月 9 日

0 投票

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 件のコメント

James
James 2012 年 8 月 9 日
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 2012 年 8 月 9 日
編集済み: Star Strider 2012 年 8 月 9 日
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 2012 年 8 月 10 日
Okay, I understand things better now. Please explain this line: P = [ones(size(Blue)) Green Red]\Blue;
Star Strider
Star Strider 2012 年 8 月 10 日
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 2012 年 8 月 12 日
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 2012 年 8 月 12 日
My pleasure!
I learned a lot in the process as well.

サインインしてコメントする。

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 9 日
編集済み: Azzi Abdelmalek 2012 年 8 月 9 日

0 投票

%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 件のコメント

James
James 2012 年 8 月 9 日
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.

サインインしてコメントする。

Kartik Verma
Kartik Verma 2020 年 12 月 1 日

0 投票

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.

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

質問済み:

2012 年 8 月 9 日

回答済み:

2020 年 12 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by