필터 지우기
필터 지우기

How to delete a zeros in matrix?

조회 수: 27 (최근 30일)
Matlab111
Matlab111 2015년 2월 7일
댓글: Matlab111 2015년 2월 8일
I have a matrix like this
a=[ 0.7446 0.0364 186.1153 0 134.5022 114.8212 84.7745 130.8661
0 0 13.8840 0 16.1683 10.4461 69.8035 114.2774];
i want to delete zeros and their corresponding values. The solution should be like this...
b= [186.1153 134.5022 114.8212 84.7745 130.8661
13.8840 16.1683 10.4461 69.8035 114.2774];

채택된 답변

Image Analyst
Image Analyst 2015년 2월 7일
This will get rid of all columns that have a zero anywhere in them:
colsWithZeros = any(a==0)
b = a(:, ~colsWithZeros)
  댓글 수: 2
Jan
Jan 2015년 2월 7일
If a gets a matrix with a single row unexpectedly, this will fail. Better specify the dimension to operate on explicitly:
colsWithZeros = any(a==0, 1)
Matlab's smart guess to operate on the first non-singelton dimension is a keen source of many bugs.
Matlab111
Matlab111 2015년 2월 8일
Image Analyst- Thank you

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

추가 답변 (2개)

Jason Moore
Jason Moore 2015년 2월 7일
편집: Jason Moore 2015년 2월 7일
I think this question was already asked before but this code should do it.
b = a(find(a~=0))
  댓글 수: 2
Image Analyst
Image Analyst 2015년 2월 7일
Nope.
Matlab111
Matlab111 2015년 2월 8일
Thank you

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


ulgi onor
ulgi onor 2015년 2월 7일
a(a==0)=[]
a =
Columns 1 through 9 0.7446 0.0364 186.1153 13.8840 134.5022 16.1683 114.8212 10.4461 84.7745 Columns 10 through 12 69.8035 130.8661 114.2774
  댓글 수: 2
David Young
David Young 2015년 2월 7일
I think that what's wanted is a 2-row matrix with the relevant columns deleted.
This answer doesn't deliver that - it shares the same problem as Jason Moore's.
David Young
David Young 2015년 2월 7일
Also note that your result has different elements to the matrix b in the question.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by