how to return indices?

조회 수: 1 (최근 30일)
Lilya
Lilya 2022년 6월 12일
댓글: Lilya 2022년 6월 12일
Hi,
I have a 3 dimensional data matrix (72*45*2109), and I need to extract all data less than 15. the final output should be a 3d matrix
I used the following lines, but the result is a vector. The point is to return the indices to have 3d matrix.
Any help would be greatly appreciated.
[r,c,v] = ind2sub(size(data),find(data >= 15));
l = data(sub2ind(size(data),r,c,v));
  댓글 수: 2
DGM
DGM 2022년 6월 12일
편집: DGM 2022년 6월 12일
You get a vector, because those are the indices of elements that you wanted -- only the indices you wanted.
Arrays need to be rectangular, without holes.
With that restriction, you'll have to decide what you want to fill all the area where there are elements you didn't want. It could be zero, NaN, etc. It's up to you and what the subequent code can accept without causing errors, since neither zero nor NaN are valid indices.
Lilya
Lilya 2022년 6월 12일
thanks for your comment
I've created an output nan matrix with the same dimension (72*45*2109), but all the output (r,c,v) are vectros as well
I don't know how to put them back in the output matrix

댓글을 달려면 로그인하십시오.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2022년 6월 12일
Based on your comment -
%random data
y=randi(30,2,3,4)
y =
y(:,:,1) = 7 17 10 24 11 14 y(:,:,2) = 3 2 6 13 17 1 y(:,:,3) = 26 3 14 26 27 3 y(:,:,4) = 7 5 4 3 7 26
z=NaN(2,3,4);
z(y<15)=y(y<15)
z =
z(:,:,1) = 7 NaN 10 NaN 11 14 z(:,:,2) = 3 2 6 13 NaN 1 z(:,:,3) = NaN 3 14 NaN NaN 3 z(:,:,4) = 7 5 4 3 7 NaN
  댓글 수: 1
Lilya
Lilya 2022년 6월 12일
Thank you very much!!
perfectly worked.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by