How to refer a high dimension matrix
1 次查看(过去 30 天)
显示 更早的评论
I have a high dimensional matrix A, two row vectors ind1 and ind2. The length of ind1 and ind2 is flexible but
length(size(A))==length(ind1)+length(ind2)
is always true. So how can I refer to the element of A whose index is (ind1(1),...ind1(end),ind2(1),...ind2(end))? Since the length of ind1 and ind2 is flexible, I couldn't simply write out every index.
0 个评论
采纳的回答
Stephen23
2015-7-7
编辑:Stephen23
2015-7-7
This is exactly what sub2ind is for, it generates the linear indices corresponding to subscript indices. Try doing something like this (untested):
ids = num2cell([ind1,ind2]);
idx = sub2ind(size(A),ids{:});
A(idx) % access that element
3 个评论
James Tursa
2015-7-7
编辑:James Tursa
2015-7-7
ids is a cell array, but ids{:} is a comma separated list. E.g., see this link:
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!