How can I create an array of handles / pointers to matrices in Matlab?
I have a bunch of related matrices of different sizes, and would like to be able to incrementally access them. Is there an easy way to create a vector of handles or pointers to these matrices in Matlab? Or is this not the way I am supposed to do it?
For example, here I want to assign to the vector indexed with i, which will be a handle to the matrices of different size.
rows 开发者_如何学运维= [1:6];
columns = [10:2:20];
for i=1:6
vector_of_pointers(i) = ones(rows(i),columns(i));
end
In Matlab, there aren't really pointers.
Instead, you can collect the arrays in a cell array, like so
rows = [1:6];
columns = [10:2:20];
for i=1:6
arrayOfArrays{i} = ones(rows(i),columns(i));
end
To access, say, array #3, you write arrayOfArrays{3}
, and if you want its second row only, you write arrayOfArrays{3}(2,:)
.
You can also create your array using ARRAYFUN
arrayOfArrays = arrayfun(@(u,v)ones(u,v),rows,columns,'uniformOutput',false)
精彩评论