how to plot only one iteration in for loop?

A=[2 2 2; 3 3 3; 1 1 1];
B=[1 2 3];
As you can see it bellow, this loop consist of 3 iterations. Can i ask Matlab to plot the second case or third case only? if its possible, please tell me how?
for i = 1:3
C = B + A(1:end,i)
end
C1 =
3
5
4
C2 =3
5
4
C3 =3
5
4
plot (x,C2)
or
plot (x,C3)
Thank you guys for your help, I appreciate any help from you

댓글 수: 2

plot b/w what?. Form your question clearly...
Brwa
Brwa 2013년 5월 29일
I have edited the question, i hope its been clear enough to understand

댓글을 달려면 로그인하십시오.

답변 (4개)

David Sanchez
David Sanchez 2013년 5월 29일

0 개 추천

for i = 1:3
C = B + A(1:end,i);
if i==2
disp(C);
end
end

댓글 수: 2

Brwa
Brwa 2013년 5월 29일
Hi Mr David thanks for your help again, but what if i have a big loop such as
for i =1:100
is there a better way to do it?
Image Analyst
Image Analyst 2013년 5월 29일
편집: Image Analyst 2013년 5월 29일
That is not a big loop. Now if you get into the tens of millions of iterations, then you're getting big, but 100 is far, far from a "big loop" so don't worry about it. There is an unjust fear of loops going around. For example, look at this loop where I iterate a million times:
tic;
count = 0;
for k = 1 : 1000000
count = count + 1;
end
toc
Now look at the elapsed time:
Elapsed time is 0.0071 seconds.

댓글을 달려면 로그인하십시오.

Muruganandham Subramanian
Muruganandham Subramanian 2013년 5월 29일
편집: Muruganandham Subramanian 2013년 5월 29일

0 개 추천

what is x?..see while plotting the size of xlabel and ylabel should be same.. In ur code x might be
x=[1 2 3];
If its right, then you can do it by
for i = 1:3
C = B + A(1:end,i);
if i~=1
plot(x,C);
end
end

댓글 수: 1

Brwa
Brwa 2013년 5월 31일
편집: Brwa 2013년 5월 31일
Dear Muruganandham Subramanian you are right, your code is very simple and almost the same as David Sanchez's code, but my problem is, i only know what i want to plot after the end of loop or after i know the result of all C1, C2, Cn.
After knowing the result probabily i will be interested to plot C2 or C3 it depend on the result.
so if x = [ 1 2 3]
for i = 1:3
C = B + A(1:end,i);
end
Ans
C1
C2
C3
now i can decide which one i should plot.
maybe i want to plot (x,C2)
Note C1 not equal C2 not equal C3, in my real code, I just dont put my real code because its too long its consist of several M.files
Thanks brother, Thanks to all of you. I really dont know how to appreciate your helps and your kindness. i hope someday i can help people with my knowledge too.

댓글을 달려면 로그인하십시오.

Andrei Bobrov
Andrei Bobrov 2013년 5월 29일

0 개 추천

id = [4 10 26 78]; % Let id case's use for the plotting.
for i1 = 1:100
C = B + A(1:end,i1);
if ismember(i1,id)
plot(x,C);
end
end

댓글 수: 1

Brwa
Brwa 2013년 5월 31일
Thanks Mr Andrei Bobrv. I always appreciate your help

댓글을 달려면 로그인하십시오.

Brwa
Brwa 2013년 5월 31일

0 개 추천

Thanks you guys, I collected all of your ideas then I come out with new code which can do my job.
Here is the code
A=[2 4 2; 3 4 3; 1 1 1]
B=[1; 2; 3];
x =[1 2 3];
for i = 1:3
C = B + A(:,i);
end
Ans =
C1
C2
C3
Now, if i want to plot C2 then i have to make another loop. As I said i can only make sure what do i want to plot after all results of C from the first loop.
for i = 1:3
C = B + A(:,i);
C
if i==2
plot(x,C);
end
end
If you have better idea please share it with us.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

질문:

2013년 5월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by