matlab - How iterate with a certain value? -
i newbie in matlab. want find way in python iterate on values.
in python that:
for in [0, 90, 180, 360]: print('degree', i)
but, how in matlab?
short version:
do this:
myvar = [0, 90, 180, 360]; = 1:size(myvar, 2) ival = myvar(:, i); disp(['degree', num2str(ival)]); end
long version:
matlab doesn't have equivalent python iterators. loop rules fundamentally different.
your code work matlab written in python:
for = [0, 90, 180, 360] disp(['degree', num2str(i)]); end
however, because special case in matlab that, coincidence, happens have similar result 1 in python. matlab doing different, seemingly similar code work in python silently different thing in matlab.
python's loop rules depend on iterator protocol, iterable classes lists , numpy arrays follow. generally, loops on "outer" sequence. in nested lists, outermost list. in numpy arrays, last dimension.
matlab's loop rules follow: flattens matrix 2d matrix if has more 2 dimension, loops on second dimension. in other words, n
th time through loop gives n
th column of loop variable, after loop variable has been flattened 2d matrix. in case, have 2d matrix first dimension 1
, second dimension 4
. loops on second dimension, giving 4 1x1
matrices (there no such thing 1d or 0d data types in matlab there in python).
however, if had column vector, this:
for = [0; 90; 180; 360] disp(['degree', num2str(i)]); end
you error:
error using horzcat dimensions of matrices being concatenated not consistent
... because instead of getting 4 1x1
matrices 1 4x1
matrix, because original matrix 4x1
matrix , has second dimension of 1
. if start 4x5
matrix, 5 4x1
matrices. if start 4x5x6
matrices, 30 (5*6
) 4x1
matrices.
also, returns same data type got in. if start 1x4
cell array, 1x1
cell arrays out, rather whatever in cell array. there isn't point using cell arrays directly, either.
overall, means using variable directly in loop, this:
for = myvar % end
...is extremely dangerous in matlab. works reliably if can absolutely 100% sure myvar
row vector. if try expand code, or use in new situation, can silently wrong thing. , other matlab internal features , functions silently wrong thing, easy sort of code seem work fine giving wrong result.
so see sort of approach in matlab, people use index variable. create sequence of indices in loop , loop on those. common approach this
myvar = [0, 90, 180, 360]; = 1:length(myvar) ivar = myvar(i); end
so in case i
index of myvar
want use. approach, although common, dangerous. unlike numpy
len
gives consistent dimension (the first one), length
in matlab gives longest dimension, whatever might be. acts unpredictably if given 2d matrices. , because matlab treats single indices indices of flattened version of matrix, again silently wrong thing.
so safe way of doing manually specify dimension want using size
function, or if want loop on values in multidimensional array use numel
function total number of elements:
for i=1:size(myvar, 2) ivar = myvar(:, i); end i=1:numel(myvar) ivar = myvar(i); end
Comments
Post a Comment