Creating a list of matrices [duplicate] - matlab

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

Related

Iterate over variables in MatLab .mat file [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.

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.

MATLAB: how to reuse plot options? [duplicate]

This question already has answers here:
Is that possible to assemble several options and pass to the plot function in matlab
(4 answers)
Closed 7 years ago.
I have the following plot:
patch('Vertices',rocket_point_cloud,'Faces',rocket_faces,...
'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1)
I would like to reuse the plot options, i.e. reuse:
'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1
Is it somehow possible to store the above in a variable, e.g. my_options and later on do:
patch('Vertices',other_cloud,'Faces',other_faces,my_options)
Thanks for your help!
Sure. Just define your options in a cell array,
my_options = {'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1};
and then expand that cell array into a comma-separated list via curly-brace indexing:
patch('Vertices', rocket_point_cloud, 'Faces', rocket_faces, my_options{:})

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>

update struct via another struct in Matlab [duplicate]

This question already has answers here:
What are some efficient ways to combine two structures in MATLAB?
(5 answers)
Closed 8 years ago.
I'm wondering if there is a convenient way to update a struct with the values of another struct in Matlab.
Here is the code, with the use of fieldnames, numel and a for loop,
fn = fieldnames(new_values);
for fi=1:numel(fn)
old_struct.(fn{fi}) = new_values.(fn{fi});
end
Of course, I don't want to loose the fields in old_struct that are not in new_values, so I can't use the simple old_struct=new_values.
Updating a struct is something we may want to do in a single short line in an interpreter.
Since you are convinced that there is no simpler way to achieve what you want, here is the method described in Loren Shure's article (see link posted in Dan's comment), applied to your example:
%// Remove overlapping fields from first struct
s_merged = rmfield(s_old, intersect(fieldnames(s_old), fieldnames(s_new)));
%// Obtain all unique names of remaining fields
names = [fieldnames(s_merged); fieldnames(s_new)];
%// Merge both structs
s_merged = cell2struct([struct2cell(s_merged); struct2cell(s_new)], names, 1);
Note that this slightly improved version can handle arrays of structs, as well as structs with overlapping field names (this is what I believe you call collision).