How to use 'for loop' for plotting multiple graphs?

I am kind of stuck in this code how to use nested for loop for x & y my below code:
x = 1:1:10;
y = 1:1:10;
[b, c] = meshgrid (0:0.125:100);
for z = x
for m = y
a= 100*(x-1) + (1-x)*b + (y-x)*c;
a(~(b+c<=100)) = NaN;
a(~(b>=0)) = NaN;
a(~(c>=0)) = NaN;
surf(b, c, a, 'EdgeColor', 'none')
hold on
end
end
Can someone help me?

Antworten (1)

KSSV
KSSV am 17 Mai 2017

0 Stimmen

x=[2 3 4];
y=[1.5 2.5 3.5];
n = length(x) ;
for i = 1:n
[b, c] = meshgrid (0:0.125:100);
a= 100*(x(i)-1) + (1-x(i))*b + (y(i)-x(i))*c;
a(~(b+c<=100)) = NaN;
a(~(b>=0)) = NaN;
a(~(c>=0)) = NaN;
surf(b, c, a, 'EdgeColor', 'none')
hold on
end

5 Kommentare

K R
K R am 17 Mai 2017
Could you please look into the code i have tried and update it accordingly, thanks.
x = 1:1:10;
y = 1:1:10;
[b, c] = meshgrid (0:0.125:100);
figure
hold on
for i = 1:length(x)
for j = 1:length(y)
a= 100*(x(i)-1) + (1-x(i))*b + (y(j)-x(i))*c;
a(~(b+c<=100)) = NaN;
a(~(b>=0)) = NaN;
a(~(c>=0)) = NaN;
surf(b, c, a, 'EdgeColor', 'none')
end
end
Check whether (x,y) loop is 1D (points) or 2D (meshgrid).
K R
K R am 17 Mai 2017
it's plotting it as 2d. I need it as 3d. Also it's consuming a lot of time to show the output for 1 to 10
KSSV
KSSV am 17 Mai 2017
YOu just gave and asked the question.....you should explain the question...:)
@K R: "I need it as 3D": You forgot to mention this in the question. "3D" is not a unique explanation, because this coud be a mesh, a surface, a path, a waterfall, a contour plot, perhaps some slices or a scatter plot. Of course creating a huge number of surf objects needs a lot of time. If you want something else, please explain this in detail.
a(b<0) % is faster and nicer than:
a(~(b>=0))

Melden Sie sich an, um zu kommentieren.

Gefragt:

K R
am 17 Mai 2017

Kommentiert:

Jan
am 17 Mai 2017

Community Treasure Hunt

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

Start Hunting!

Translated by