I want to plot a set of points satisfying certain condition. Although, there are many points in this set satisfying the given condition, the code I am using plots only the last point. Anyone can help me to plot all these points?

1 Ansicht (letzte 30 Tage)
m=8;
for k=1:m;
for n=1:m;
v=n+k;
if v<= 7
plot(k,n,'b');
k
n
v
else
end
end
end

Akzeptierte Antwort

Daniel Sahlin
Daniel Sahlin am 21 Dez. 2017
Hi Mohammad Ali, You could probably just set a “hold on” statement after the plot, and change the style to e.g. ‘bo’ to get the individual points on the same graph.
plot(k,n,'bo'); hold on
It might however be worth considering saving k & n in vectors and making the plot after the loops depending on the application.
I hope it helps, Daniel

Weitere Antworten (1)

Are Mjaavatten
Are Mjaavatten am 21 Dez. 2017
You should specify a marker, since otherwise Matlab tries to plot a line between points. With only one point for each plot statement no line is drawn. Also, unless you instruct Matlab to "hold" the existing plot, the new plot command will clear the existing plot. Below, I have modified your code to use a ring ('o') as a marker.
m=8;
for k=1:m;
for n=1:m;
v=n+k;
if v<= 7
plot(k,n,'ob');
hold on
else
end
end
end

Kategorien

Mehr zu Line Plots 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!

Translated by