필터 지우기
필터 지우기

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
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
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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by