필터 지우기
필터 지우기

Remove rows of zeroes, and extract the removed rows indices

조회 수: 1 (최근 30일)
czeslaw
czeslaw 2017년 3월 4일
댓글: Star Strider 2017년 3월 4일
Hi all,
I have T =
0 0 0 0
1 2 0 4
5 6 0 8
0 0 0 0
10 11 0 12
t2=T(any(T,2),:) will give
t2 =
1 2 0 4
5 6 0 8
10 11 0 12
But how can I efficiently retrieve the indices of the removed rows from T, that is: 1,4 ?? Then how can I efficiently retrieve the indices of the not removed rows from T, that is: 2,3,5 ?? Without using while, for loop etc.
Thank you in advance.

채택된 답변

Star Strider
Star Strider 2017년 3월 4일
편집: Star Strider 2017년 3월 4일
This works:
T = [0 0 0 0
1 2 0 4
5 6 0 8
0 0 0 0
10 11 0 12];
RemovedRows = all(T == 0, 2)
RemovedRowsIdx = find(RemovedRows)
KeptRowsIdx = find(any(T,2))
RemovedRows =
5×1 logical array
1
0
0
1
0
RemovedRowsIdx =
1
4
KeptRowsIdx =
2
3
5
EDIT Remembered ‘KeptRowsIdx’.
  댓글 수: 4
Image Analyst
Image Analyst 2017년 3월 4일
If speed is what was needed, you shouldn't even use find() at all. I doubt he really needed the actual row numbers - that's why I didn't even bother to compute them. I think it's just the final matrix that is wanted. He'll probably never even use the row numbers again - so use logical indexing, which you already have, rather than doing the additional step to get linear indexing.
Star Strider
Star Strider 2017년 3월 4일
@Image Analyst —
Possibly, but that wasn’t the impression I got.
Quoting:
  • ‘But how can I efficiently retrieve the indices of the removed rows from T, that is: 1,4 ??Then how can I efficiently retrieve the indices of the not removed rows from T, that is: 2,3,5 ??’
The index numbers appear to be requested.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 3월 4일
Try this:
rowsToKeep = ~all(T == 0, 2)
t2 = T(rowsToKeep, :)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by