如何在Matlab中创建矩阵的句柄/指针数组? [英] How can I create an array of handles / pointers to matrices in Matlab?
问题描述
我有一堆不同大小的相关矩阵,希望能够逐步访问它们.有没有一种简单的方法可以在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?
例如,在这里我想分配给索引为i的向量,这将是不同大小的矩阵的句柄.
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
推荐答案
在Matlab中,实际上并没有指针.
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
要访问例如数组3,请写arrayOfArrays{3}
,如果只希望它的第二行,请写arrayOfArrays{3}(2,:)
.
To access, say, array #3, you write arrayOfArrays{3}
, and if you want its second row only, you write arrayOfArrays{3}(2,:)
.
您还可以使用 ARRAYFUN
arrayOfArrays = arrayfun(@(u,v)ones(u,v),rows,columns,'uniformOutput',false)
这篇关于如何在Matlab中创建矩阵的句柄/指针数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!