Expanding a 2D matrix to 3D

7 ビュー (過去 30 日間)
Camille Woicekowski
Camille Woicekowski 2020 年 7 月 17 日
編集済み: Matt J 2020 年 7 月 17 日
I have a 2D matrix (3000x22) of True/False values (zeros and ones). Each column contains three True values. I want to make this into a 3D matrix (3000x20x3) where the first new dimmension has the first True value, the second has the second True value, and the third has the third True value. How can I do this?

採用された回答

Matt J
Matt J 2020 年 7 月 17 日
編集済み: Matt J 2020 年 7 月 17 日
Another way,
C=double(A); %A is the given matrix
C(logical(A))=mod(0:nnz(A)-1,3)+1;
result = cat(3, C==1, C==2, C==3)
  1 件のコメント
Camille Woicekowski
Camille Woicekowski 2020 年 7 月 17 日
This worked perfectly, thank you!

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2020 年 7 月 17 日
編集済み: Matt J 2020 年 7 月 17 日
Let's call your given matrix, A,
[m,n]=size(A);
[I,J]=find(A);
K=repmat((1:3).',1,n);
result=accumarray([I,J,K(:)],true,[m,n,3]);

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by