I think this is what the OP really wants:
array = -1:0.1:10
for i=1:numel(array)
disp(array(i))
end
Zach is correct about the direct answer to the question.
An interesting side note is that the following two loops do not execute the same:
for i=1:10000
% do something
end
for i=[1:10000]
% do something
end
The first loop creates a variable i
that is a scalar and it iterates it like a C for loop. Note that if you modify i
in the loop body, the modified value will be ignored, as Zach says. In the second case, Matlab creates a 10k-element array, then it walks all elements of the array.
What this means is that
for i=1:inf
% do something
end
works, but
for i=[1:inf]
% do something
end
does not (because this one would require allocating infinite memory). See Loren's blog for details.
Also note that you can iterate over cell arrays.
If you are trying to loop over a cell array and apply something to each element in the cell, check out cellfun
. There's also arrayfun
, bsxfun
, and structfun
which may simplify your program.
Let's say you have an array of data:
n = [1 2 3 4 6 12 18 51 69 81 ]
then you can 'foreach' it like this:
for i = n, i, end
This will echo every element in n (but replacing the i with more interesting stuff is also possible of course!)
ooh! neat question.
Matlab's for loop takes a matrix as input and iterates over its columns. Matlab also handles practically everything by value (no pass-by-reference) so I would expect that it takes a snapshot of the for-loop's input so it's immutable.
here's an example which may help illustrate:
>> A = zeros(4); A(:) = 1:16
A =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
>> i = 1; for col = A; disp(col'); A(:,i) = i; i = i + 1; end;
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> A
A =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
ooh! neat question.
Matlab's for loop takes a matrix as input and iterates over its columns. Matlab also handles practically everything by value (no pass-by-reference) so I would expect that it takes a snapshot of the for-loop's input so it's immutable.
here's an example which may help illustrate:
>> A = zeros(4); A(:) = 1:16
A =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
>> i = 1; for col = A; disp(col'); A(:,i) = i; i = i + 1; end;
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> A
A =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
The MATLAB for loop basically allows huge flexibility, including the foreach functionality. Here some examples:
1) Define start, increment and end index
for test = 1:3:9
test
end
2) Loop over vector
for test = [1, 3, 4]
test
end
3) Loop over string
for test = 'hello'
test
end
4) Loop over a one-dimensional cell array
for test = {'hello', 42, datestr(now) ,1:3}
test
end
5) Loop over a two-dimensional cell array
for test = {'hello',42,datestr(now) ; 'world',43,datestr(now+1)}
test(1)
test(2)
disp('---')
end
6) Use fieldnames of structure arrays
s.a = 1:3 ; s.b = 10 ;
for test = fieldnames(s)'
s.(cell2mat(test))
end
Zach is correct about the direct answer to the question.
An interesting side note is that the following two loops do not execute the same:
for i=1:10000
% do something
end
for i=[1:10000]
% do something
end
The first loop creates a variable i
that is a scalar and it iterates it like a C for loop. Note that if you modify i
in the loop body, the modified value will be ignored, as Zach says. In the second case, Matlab creates a 10k-element array, then it walks all elements of the array.
What this means is that
for i=1:inf
% do something
end
works, but
for i=[1:inf]
% do something
end
does not (because this one would require allocating infinite memory). See Loren's blog for details.
Also note that you can iterate over cell arrays.
ooh! neat question.
Matlab's for loop takes a matrix as input and iterates over its columns. Matlab also handles practically everything by value (no pass-by-reference) so I would expect that it takes a snapshot of the for-loop's input so it's immutable.
here's an example which may help illustrate:
>> A = zeros(4); A(:) = 1:16
A =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
>> i = 1; for col = A; disp(col'); A(:,i) = i; i = i + 1; end;
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> A
A =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
If you are trying to loop over a cell array and apply something to each element in the cell, check out cellfun
. There's also arrayfun
, bsxfun
, and structfun
which may simplify your program.
Zach is correct about the direct answer to the question.
An interesting side note is that the following two loops do not execute the same:
for i=1:10000
% do something
end
for i=[1:10000]
% do something
end
The first loop creates a variable i
that is a scalar and it iterates it like a C for loop. Note that if you modify i
in the loop body, the modified value will be ignored, as Zach says. In the second case, Matlab creates a 10k-element array, then it walks all elements of the array.
What this means is that
for i=1:inf
% do something
end
works, but
for i=[1:inf]
% do something
end
does not (because this one would require allocating infinite memory). See Loren's blog for details.
Also note that you can iterate over cell arrays.
I think this is what the OP really wants:
array = -1:0.1:10
for i=1:numel(array)
disp(array(i))
end
As of today (Feb 27), there is a new For-Each toolbox on the MATLAB File Exchange that accomplishes the concept of foreach
. foreach
is not a part of the MATLAB language but use of this toolbox gives us the ability to emulate what foreach
would do.
The MATLAB for loop basically allows huge flexibility, including the foreach functionality. Here some examples:
1) Define start, increment and end index
for test = 1:3:9
test
end
2) Loop over vector
for test = [1, 3, 4]
test
end
3) Loop over string
for test = 'hello'
test
end
4) Loop over a one-dimensional cell array
for test = {'hello', 42, datestr(now) ,1:3}
test
end
5) Loop over a two-dimensional cell array
for test = {'hello',42,datestr(now) ; 'world',43,datestr(now+1)}
test(1)
test(2)
disp('---')
end
6) Use fieldnames of structure arrays
s.a = 1:3 ; s.b = 10 ;
for test = fieldnames(s)'
s.(cell2mat(test))
end
When iterating over cell arrays of strings, the loop variable (let's call it f
) becomes a single-element cell array. Having to write f{1}
everywhere gets tedious, and modifying the loop variable provides a clean workaround.
% This example transposes each field of a struct.
s.a = 1:3;
s.b = zeros(2,3);
s % a: [1 2 3]; b: [2x3 double]
for f = fieldnames(s)'
s.(f{1}) = s.(f{1})';
end
s % a: [3x1 double]; b: [3x2 double]
% Redefining f simplifies the indexing.
for f = fieldnames(s)'
f = f{1};
s.(f) = s.(f)';
end
s % back to a: [1 2 3]; b: [2x3 double]
Let's say you have an array of data:
n = [1 2 3 4 6 12 18 51 69 81 ]
then you can 'foreach' it like this:
for i = n, i, end
This will echo every element in n (but replacing the i with more interesting stuff is also possible of course!)
ooh! neat question.
Matlab's for loop takes a matrix as input and iterates over its columns. Matlab also handles practically everything by value (no pass-by-reference) so I would expect that it takes a snapshot of the for-loop's input so it's immutable.
here's an example which may help illustrate:
>> A = zeros(4); A(:) = 1:16
A =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
>> i = 1; for col = A; disp(col'); A(:,i) = i; i = i + 1; end;
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> A
A =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
As of today (Feb 27), there is a new For-Each toolbox on the MATLAB File Exchange that accomplishes the concept of foreach
. foreach
is not a part of the MATLAB language but use of this toolbox gives us the ability to emulate what foreach
would do.
When iterating over cell arrays of strings, the loop variable (let's call it f
) becomes a single-element cell array. Having to write f{1}
everywhere gets tedious, and modifying the loop variable provides a clean workaround.
% This example transposes each field of a struct.
s.a = 1:3;
s.b = zeros(2,3);
s % a: [1 2 3]; b: [2x3 double]
for f = fieldnames(s)'
s.(f{1}) = s.(f{1})';
end
s % a: [3x1 double]; b: [3x2 double]
% Redefining f simplifies the indexing.
for f = fieldnames(s)'
f = f{1};
s.(f) = s.(f)';
end
s % back to a: [1 2 3]; b: [2x3 double]
Source: Stackoverflow.com