Fixing loop values while plotting
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello All,
I have been practicing matlab for fun for sometime now and I am getting into plotting data structures at the moment. I have been trying to "fix" certain loop values while plotting but I have been having a hard time getting this to work. In the code below, I would like to plot all the 'b" values for y=1 and z=1 while x=1:3. I can easily do this by fixing one of the variables but I currently can not fix y and z at the same time. Please see my simple code below. Any advice would be greatly appreciated! Thank you!
vals.y=[];
vals.x=[];
vals.b=[];
vals.z=[];
for z = 1:3;
for y = 1:3;
for x=1:3;
b = x*y*z;
vals.y(end+1)=y;
vals.x(end+1)=x;
vals.b(end+1)=b;
vals.z(end+1)=z;
end
end
end
plot(vals.b(vals.y==1),vals.x(vals.y==1));
%plot(vals.b(vals.y==1 & vals.z==1,vals.x(vals.y==1 & vals.z==1)); - Would Like to this
% to this
0 Kommentare
Antworten (1)
Shunichi Kusano
am 24 Apr. 2019
Your code almost works. You need a closing parenthesis.
plot(vals.b(vals.y==1 & vals.z==1,vals.x(vals.y==1 & vals.z==1)) % your code
plot(vals.b(vals.y==1 & vals.z==1),vals.x(vals.y==1 & vals.z==1)) % corrected code
And one suggestion. It's a good practice that you do not use for-loop, if you can. In this case, The following code is prefarable. This is easier to write and faster to calculate.
[vals.x, vals.y, vals.z] = meshgrid(1:3);
b = vals.x .* vals.y .* vals.z;
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!