I'm looking for a quick, simple way of accessing specific arrays that are inside cell arrays. For example, let's say I have
A = rand(10,2);
B = rand(15,1);
C = rand(130,1);
D = rand(16,1);
E = rand(1000,25);
my_cell = {A,B,C,D,E};
Let's say I want the 1st, 2nd, and 4th matrices only (i.e., A, B and D) inside a new cell array. So the new cell array would be composed of {A, B, D}. This is obviously easy using a for loop:
idx=[1,2,4];
new_cell=cell(1,length(idx));
for i=1:length(idx)
new_cell{i}=my_cell{idx(i)};
end
I was wondering if there was an even quicker/simpler way. Maybe there's an obivous indexing trick or function I'm not aware of? I'd appreciate the help.
{my_cell{idx}}
should do the trick.
my_cell{idx}
returns the elements in my_cell indexed by idx as a comma separated list. It is equivalent to A, B, D
. All you need to do is to close this list with {}
to make a cell array out of it.