how to index a matrix by using a index matrix that has same size?
2 ビュー (過去 30 日間)
古いコメントを表示
I have a m by n data matrix and a m by n index matrix which rearranges the order of the n elements in each row of the data matrix. How can I get an indexed data matrix without using loop? Thanks.
1 件のコメント
採用された回答
Walter Roberson
2013 年 3 月 30 日
編集済み: Walter Roberson
2013 年 3 月 30 日
Use sub2ind()
3 件のコメント
Walter Roberson
2013 年 3 月 30 日
Oops, yes, I was concentrating on not putting an "s" in and so left out the "2" :(
Cedric
2013 年 3 月 30 日
I thought that you had forgotten the "s" and I ended up realizing that it was a "2" ;-)
その他の回答 (2 件)
Anand
2013 年 3 月 30 日
If A is your original matrix and idx is the matrix of indices, you can use logical indexing: A(idx).
Here's an example:
>> A = rand(3)
A =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
>> idx = [9 8 7;6 5 4;3 2 1]
idx =
9 8 7
6 5 4
3 2 1
>> A(idx)
ans =
0.9575 0.5469 0.2785
0.0975 0.6324 0.9134
0.1270 0.9058 0.8147
1 件のコメント
参考
カテゴリ
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!