Iterate over variables in MatLab .mat file [duplicate] - matlab

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I iterate through each element in an n-dimensional matrix in MATLAB?
I have a column vector list which I would like to iterate like this:
for elm in list
//do something with elm
How?

In Matlab, you can iterate over the elements in the list directly. This can be useful if you don't need to know which element you're currently working on.
Thus you can write
for elm = list
%# do something with the element
end
Note that Matlab iterates through the columns of list, so if list is a nx1 vector, you may want to transpose it.

for i=1:length(list)
elm = list(i);
//do something with elm.

with many functions in matlab, you don't need to iterate at all.
for example, to multiply by it's position in the list:
m = [1:numel(list)]';
elm = list.*m;
vectorized algorithms in matlab are in general much faster.

If you just want to apply a function to each element and put the results in an output array, you can use arrayfun.
As others have pointed out, for most operations, it's best to avoid loops in MATLAB and vectorise your code instead.

Related

How to index a temporary matrix? [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 6 years ago.
When I want to access a specific element of a matrix, I use indexing with parentheses:
m = calc_stuff(...);
x = m(index1, index2);
However, I often want to do that in one line of code, like this:
x = calc_stuff(...)(index1, index2);
How can I express it?
A specific example:
m = cumsum(rand(10,4));
x = m(10, 1);
The above script calculates some sums of random variables, and then I take one example value out of the result matrix.
How could I write it as one line? The following doesn't work:
x = cumsum(rand(10,4))(10, 1);
Error: ()-indexing must appear last in an index expression.
Here, I want a general syntax, which is applicable for any calculation, not necessarily involving random variables.
You may want to check out the "Functional Programming Constructs" on the FileExchange).
Especially the file paren.m does what you need. So you would write
x = paren( cumsum(rand(10,4)), 10, 1 );
Perhaps not as elegant as the direct "()"notation, but that is not supported in MATLAB in the way you would like to use it.

Save results in a nested for loop [duplicate]

This question already has answers here:
Save loop data in vector
(2 answers)
Closed 7 years ago.
I want to save the following for-loop results in a new matrix using Matlab, how can I do that or any other suggestions?
Where X is a 5467-by-513 matrix , id is a 143-by-1 vector and wkno is a 44-by-1 vector
for i=1:size(id,1);
for j=1:size(wkno,1);
tst= X(:,1)==id(i) & X(:,2)==wkno(j);
M=mean(X(tst,:));
end
end
Just make sure you actually save the things to a matrix instead of a scalar-variable, i.e. add the subscript indices to the variable you're saving to:
for ii=1:size(id,1);
for jj=1:size(wkno,1);
tst(ii,jj)= X(:,1)==id(ii,1) & X(:,2)==wkno(jj,1);
M(ii,jj)=mean(X(tst,:));
end
end
Not that I refrained from using i and j as a variable, since this is a bad idea. I added the ,1 to id and wkno, to make sure you use them as column variables. This is a good habit to get into, because single indices will go wrong when you have a multi-dimensional array.

In Matlab, how to quickly select single element of matrix produced by a function? [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 8 years ago.
E.g. I have the output of cov(A,B), which is a 2×2 matrix.
I want to select the element in position 2,1 of the matrix.
I can do this by blah = cov(A,B) and then select blah(1,2).
This isn't the most efficient way to do it though, and I'd prefer to do it in one line. Is there a way to do that?
You can try using getfield():
getfield(cov(A,B), {1,2})
The performance difference between this and what you have currently will likely be negligible, however. I personally would prefer just using that temporary variable.
<stealing brilliance from Amro>
You can also do this:
C = builtin('_paren', cov(A,B), 2, 1);
</stealing brilliance from Amro>

Creating a list of matrices [duplicate]

This question already has answers here:
Array of Matrices in MATLAB
(6 answers)
Closed 9 years ago.
I am new to programming and I was wondering if my question has a simple implementation. I have a bunch of matrices and I want a way to be able to store them, or be able to easily call them and do operations on them. For example, if I have 100 matrices, called, M1,M2,...M100; is there a way I can rename them so that if I want to call the nth matrix, I can just write M(nth)?
EDIT:
For example, if I want to add M1+M1, M1+M2, ...,M1+M100; I want to be able to write a loop something kind of like,
for i=1:100
AM(i)=M(1)+M(i)
end
Is this possible?
Use cell array
AM = cell(1,100);
and set it as
AM{i} = Mi;
then you can access it as
AM{i};
note the use of {} to access each element of the cell array AM, that is in turn a matrix

How to iterate over a column vector in Matlab? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I iterate through each element in an n-dimensional matrix in MATLAB?
I have a column vector list which I would like to iterate like this:
for elm in list
//do something with elm
How?
In Matlab, you can iterate over the elements in the list directly. This can be useful if you don't need to know which element you're currently working on.
Thus you can write
for elm = list
%# do something with the element
end
Note that Matlab iterates through the columns of list, so if list is a nx1 vector, you may want to transpose it.
for i=1:length(list)
elm = list(i);
//do something with elm.
with many functions in matlab, you don't need to iterate at all.
for example, to multiply by it's position in the list:
m = [1:numel(list)]';
elm = list.*m;
vectorized algorithms in matlab are in general much faster.
If you just want to apply a function to each element and put the results in an output array, you can use arrayfun.
As others have pointed out, for most operations, it's best to avoid loops in MATLAB and vectorise your code instead.