Example Complex: for,if... loop;
clear;clc;
A=[400; 900; 200; 300; 100];
k=[4;1]; % index matrix
c=[11];
e=zeros(0);
for j=0:(length(k)-1);
b(j+1,:) = A(k(j+1,:), :); % call vector from index
if b(j+1,:)>200
c=union(c,b(j+1,:));
elseif (b(j+1,:)+100)>400
e=union(e,b(j+1,:));
end
end
I try to run the simpe above code to understand: if elseif loop.
Explanation of code (for ...end):
-j=0-->k(1,:)=4-->b(4,:)=A(4,:)=300 [get value from matrix A at index 4]
-j=1-->k(2,:)=1-->b(1,:)=A(1,:)=400 [get value from matrix A at index 1]
Finally we will have result matrix b=[300;400]----> GOOD
But for the (if ....elseif ...end), i hope that the result matrix e=[400], but when I run the code matrix e=[] ?????????? Can you help me where the error?
My understanding of all loops:
-j=0-->k(1,:)=4-->b(4,:)=A(4,:)=300
if b(4,:)=300>200 ---> c=[11 300]
elseif b(4,:)=300+100=400>400 : NO--->e=[]
-j=1-->k(2,:)=1-->b(1,:)=A(1,:)=400
if b(1,:)=400>200 ---> c=[11 300 400]
elseif b(1,:)=400+100=500>400: YES --->e=[400]
Finally: e=[400] : as my understanding? (How can i fix the code to get the result as my understanding)

4 Kommentare

Jan
Jan am 30 Aug. 2017
Note that there are no "if loops". Only for and while are loops, while if branches according to a condition.
Inventing an own syntax like
-j=0-->k(1,:)=4-->b(4,:)=A(4,:)=300 [get value from matrix A at index 4]
is not useful for a discussion as long, as you do not define what the symbols mean. "-j=0-->k(1.:)=4" ? It would not be smart, if I guess, what this means. Why is the valid Matlab syntax not sufficient to explain the problem? You can assume that the readers are familiar with it.
Adam
Adam am 30 Aug. 2017
For starters j = 0 is not a valid index into an array and
e=zeros(0);
just creates an empty matrix.
Guillaume
Guillaume am 30 Aug. 2017
Bearbeitet: Guillaume am 30 Aug. 2017
@Adam, the j=0 is not a problem as all indexing is done with j+1. Of course, rather than going from 0 to numel(k)-1 and then adding one to all the values for indexing, it would be a lot simpler to just go from 1 to numel(k) and not add anything:
for j = 1:numel(k)
b(j) = A(k(j, :), :);
is a lot simpler.
ha ha
ha ha am 30 Aug. 2017
Bearbeitet: ha ha am 30 Aug. 2017
@Guillaume. good friend. You understand well.
@Adam: --> mean: refer (or we have)

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 30 Aug. 2017
Bearbeitet: Jan am 30 Aug. 2017

0 Stimmen

Do you know the debugger? You can set a break point in the first line and step through the code line by line. This will reveal directly, what happens inside the code.
In the second iteration b is the vector [300; 400]. Then:
if b(j+1,:) > 200
has a vector as condition. Note that if requires a scalar as argument, and therefore Matlab inserts this internally:
cond = (b(j+1,:) > 200);
if (all(cond(:)) && ~isempty(cond)
Here both elements of b are greater than 200, such that the code might do what you expect - by accident.
But the main problem remains, that you seem to assume, that the elseif branch is executed even if the if branch was already. But this is not the meaning of elseif.
I cannot guess, how you want to treat the problem of the vector input for the condition. But maybe it is enough already to replace if ... elseif ... end by if ... end, if ... end.
Note: The intention of the code is not clear. I guess boldly, that it can be simplified massively, perhaps by:
Ak = A(k);
c = unique([11; Ak(Ak > 200)]);
e = unique(Ak(Ak > 300));

1 Kommentar

ha ha
ha ha am 30 Aug. 2017
you are correct. just replace "if ... elseif ... end" by "if ... end, if ... end." . I will get the expected answer

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu プログラミング finden Sie in Hilfe-Center und File Exchange

Tags

Noch keine Tags eingegeben.

Gefragt:

am 30 Aug. 2017

Kommentiert:

am 30 Aug. 2017

Community Treasure Hunt

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

Start Hunting!