How can I index a matrix using an array?
3 ビュー (過去 30 日間)
古いコメントを表示
Yizhuang Alden Cheng
2019 年 7 月 31 日
編集済み: Yizhuang Alden Cheng
2019 年 7 月 31 日
Hi,
I want to create a very large logical matrix, and I have an array which has the indices of the matrix which I want to be true (with the remaining entries being false).
For example, suppose I had the following array 8x2 array:
idx = [1 1;
1 2;
3 1;
3 3;
4 2;
5 1;
5 2;
5 3];
where each row represents index for a 5x3 matrix, and I want to use this array to create the following index:
A = [1 1 0;
0 0 0;
1 0 1;
0 1 0;
1 1 1];
Is there an efficient way to do this without using loops (since in my actual application, A may have something like 1,000,000 rows and 10,000 columns)? Thanks!
0 件のコメント
採用された回答
その他の回答 (1 件)
Andrei Bobrov
2019 年 7 月 31 日
s = max(idx);
A = false(s);
A(sub2ind(s,idx(:,1),idx(:,2))) = true;
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!