For loops & lengths/size command
442 views (last 30 days)
Show older comments
Cant seem to figure out where to start. Please help!
2 Comments
julian gaviria
on 14 Mar 2022
Why does the iteration with length function never stops the iteration?
Context: Using copyfile function for copying and pasting indexed files. To note, the files are rightly copied and pasted. But the iteration never stops:
%%%
clc
clear
SourcePath ='\\nasac-m2.isis.unige.ch\m-GAubry\GAubry\YODA\RAW_DATA\Live\S008\V1\';
Participant = 'Session_2\scans\87627';
SourceFolder = fullfile([SourcePath,Participant],'RUN_1_MIST_1_0004');
DistPath ='\\nasac-m2.isis.unige.ch\m-GAubry\GAubry\YODA\jg\prp_glm\Sub_08\';
DistFolder = fullfile(DistPath,'FunRaw_S1');
if ~exist(DistFolder)
mkdir(DistFolder)
end
for i=1:length(dir(fullfile(SourceFolder,'f*')))
copyfile(fullfile(SourceFolder,'f*'),DistFolder);
end
Geoff Hayes
on 14 Mar 2022
Answers (2)
Geoff Hayes
on 18 Apr 2014
Hi Samantha,
I guess that you are asking how to go about iterating (via a for loop) over a vector or matrix or whatever using the length command or size command. Typically, I use the length command if I am iterating over the contents of a (row or column) vector:
x = [1 2 3 4 5];
if isvector(x)
for i=1:length(x)
% do something on x(i)
end
end
In the above example, length(x) would return 5. As per the documentation, if you call the length command on a matrix Y, then the result is the max(size(Y)) i.e. the greatest dimension. So if Y is a 4x4 matrix, then length(Y) is 4; if Y is a 4x8 matrix then length(Y) is 8; and if Y is a 12x3 matrix, then length(Y) is 12. (Of course if Y has more than two dimensions then the length will be the largest/max value across all dimensions.
I generally use the size command if I am iterating over the contents of a matrix:
Y = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
if ismatrix(Y)
[r,c] = size(Y);
for i=1:r
for j=1:c
% do something against Y(i,j)
end
end
end
In the above example, size(Y) returns a pair of values for the two-dimensional matrix with the first value (the number of rows) equal to 4 and the second value (the number of columns) equal to 3. If the matrix were three-dimensional, then [r,c,d] = size(Y) would retrieve the size of each dimension. (Note that if you only want the size for the nth dimension you can simply do size(Y,n) where n is the dimension of Y that you are interested in. So size(Y,1) returns the number of rows, size(Y,2) the number of columns, etc.)
An alternative to either of the above is the numel command which returns the number of elements in the vector or matrix. This is equivalent to length for a vector or the product of all dimensions in a matrix. Using the above examples, numel(x) =5 and numel(Y) =12 (since there are four rows and three columns).
Hope that this helps!
Geoff
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!