필터 지우기
필터 지우기

How to remove rows with zeros in selected columns?

조회 수: 8 (최근 30일)
Emma Kuttler
Emma Kuttler 2022년 2월 24일
편집: Scott MacKenzie 2022년 2월 24일
I have a matrix with 1363 rows and 21 columns. The first column is an identifier (1-21). I need to delete all rows that contain all zeroes with the exception of the first column. For example,
a = [ 1 0 5 0 6
2 0 0 0 0
3 3 5 9 12
4 0 0 0 0
5 6 9 1 0]
I want to produce b
b = [ 1 0 5 0 6
3 3 5 9 12
5 6 9 1 0]

채택된 답변

Scott MacKenzie
Scott MacKenzie 2022년 2월 24일
편집: Scott MacKenzie 2022년 2월 24일
There is probably a simpler solution, but this will work...
a = [ 1 0 5 0 6
2 0 0 0 0
3 3 5 9 12
4 0 0 0 0 ]
a = 4×5
1 0 5 0 6 2 0 0 0 0 3 3 5 9 12 4 0 0 0 0
zeroIdx = ~sum(~a(:,2:end) == 0,2);
a(zeroIdx,:) = []
a = 2×5
1 0 5 0 6 3 3 5 9 12

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by