简体   繁体   中英

complex matlab for loop with matrix

I don't understand this piece of code of a for loop in matlab, I know that loops in matlab usually look like: for ii=1:2:100 so that it starts in 1 until 100 and in each iteration you add 2. But here I've got this condition in the loop and I don't get what it does:

for ii=[1:w:rd(1)-w-border, rd(1)-w-border+1],
   ...
end;

w and border are integers passed as arguments and rd is size of the image/matrix ( rd = size(image); )

Can someone explain me how for loops work in matlab with this kind of condition? Thanks in advance.

the for argument is a vector. the loop iterator ii takes one value for the vector for each iteration of the loop. As you mentioned, the vector can be equally spaced one like 1:2:100 . But it can also be arbitrary, for example for ii = [4,6,1,8] ... .
In you case the for argument vector is partly "equally spaced" vector: 1:w:rd(1)-w-border plus another element rd(1)-border+1 .

For loop in matlab can execute statements for a defined set of index values: For example, the following code will display all the element in the set [1,5,8,17] :

for s = [1,5,8,17]
 disp(s)
end

Your code for ii=[1:w:rd(1)-w-border, rd(1)-w-border+1] is similar. Its just like a set 1:w:rd(1)-w-border with an additional element rd(1)-w-border+1 .

Its like writing this set [1,2,3,4,5,8] as [1:1:5, 8] I hope its clear now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM