Extract data from matrices and arrays using vectors of coordinates
65 次查看(过去 30 天)
显示 更早的评论
I have a matrix and I want to use two vectors of row and column index coordinates in order to extract the elements from it. In particular, I can define a 3 x 3 matrix as
A = [1 2 3; 3 4 5; 8 9 10]
And I want to use two vectors to extract the elements (2,2), (3,1), (1,3). My intention is to do this using the vectors for each corrdinates. I have been trying to do it by writing
A([2 3 1], [2 1 3])
but of course this is not working. I believe that this is a basic sintax issue.
Thank you so much
0 个评论
采纳的回答
the cyclist
2021-9-28
编辑:the cyclist
2021-9-28
Using your example:
A = [1 2 3; 3 4 5; 8 9 10]
linearIndex = sub2ind(size(A),[2 3 1], [2 1 3])
A(linearIndex)
更多回答(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!