How to construct a matrix values by its row or column indexes?
13 次查看(过去 30 天)
显示 更早的评论
Hi all,
I need your help on this. Suppose we have a 4x3 matrix and we need to construct a metrix with its row and column indexes as follows,
R = 1 1 1; 2 2 2; 3 3 3; 4 4 4
C= 1 2 3; 1 2 3; 1 2 3; 1 2 3
Thx in advance.
0 个评论
回答(3 个)
Evan
2013-7-9
编辑:Evan
2013-7-9
A(sub2ind(size(A),R,C))
Example:
>>A = magic(4);
>>R = [1 1 1; 2 2 2; 3 3 3; 4 4 4];
>>C = [1 2 3; 1 2 3; 1 2 3; 1 2 3];
>>B = A(sub2ind(size(A),R,C))
B =
16 2 3
5 11 10
9 7 6
4 14 15
If you're not talking about a generalized case and just want that particular indexing of any matrix, though, it looks like you just need to trim off part of the matrix:
>>B = A(1:4,1:3);
B =
16 2 3
5 11 10
9 7 6
4 14 15
2 个评论
另请参阅
类别
在 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!