フィルターのクリア

transforming a 2-dim array integers to 3-dim array of logical which are true for the corresponding number - without using a loop

1 回表示 (過去 30 日間)
Hello,
I would like to take an 2-dim array like
IND=[4 3 2;
1 2 3];
and create a 3-dim of logicals like
GOAL(:,:,1)=[0 0 0 1;
1 0 0 0]
GOAL(:,:,2)=[0 0 1 0;
0 1 0 0]
GOAL(:,:,3)=[0 1 0 0;
0 0 1 0]
So the first second dimension is newly created, it is as long as the highest number in IND and is zero(false), except for the indice, that is 1(true)
I really need it to be fast, so a for loop will not do.
Many thanks Marcus
  2 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 22 日
Give an example of your desired output
nobody
nobody 2018 年 10 月 22 日
編集済み: nobody 2018 年 10 月 22 日
output = GOAL (logical 2*4*3)
input = IND (integer 2*3)
(I will then use GOAL to get specific values in another 2*4*3 matrix)

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

採用された回答

Bruno Luong
Bruno Luong 2018 年 10 月 22 日
編集済み: Bruno Luong 2018 年 10 月 22 日
IND=[4 3 2;
1 2 3];
[I,~,K] = ndgrid(1:size(IND,1),1,1:size(IND,2));
J = IND;
GOAL = accumarray([I(:),J(:),K(:)],1);
  3 件のコメント
Bruno Luong
Bruno Luong 2018 年 10 月 22 日
Opps sorry there was a bug I fix it
nobody
nobody 2018 年 10 月 22 日
編集済み: nobody 2018 年 10 月 22 日
Works great, I will try to understand what it does now :D
Thank you very much

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by