how to delete the rest of the zeros from a matrix

조회 수: 1 (최근 30일)
wasima tammi
wasima tammi 2015년 3월 6일
댓글: Sad Grad Student 2015년 3월 24일
suppose this the output c=[0 0 0 0; 0 0 0 0; 1 1 1 1] now how could i delete the other zeros for a looop and only print the last row??
  댓글 수: 1
Adam
Adam 2015년 3월 6일
Is that as complex as the problem gets or do you have case where some 1s appear in a row with 0s? If so are you expecting the output to simply be a 1d array of basically however many 1s exist in c, irrespective of the structure of where they appear?

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

채택된 답변

Sad Grad Student
Sad Grad Student 2015년 3월 6일
편집: Sad Grad Student 2015년 3월 6일
To remove 0, you can simply do: c(~c) = []; you don't need a loop! Beauty of matlab :)
~c is essentially looking for empty values in c. In Matlab, 0 means empty value and you can simply remove it by replacing it with []
>> c=[0 0 0 0; 0 0 0 0; 1 1 1 1]
c =
0 0 0 0
0 0 0 0
1 1 1 1
>> c(~c) = []
c =
1 1 1 1
>>
  댓글 수: 2
wasima tammi
wasima tammi 2015년 3월 7일
when i m doing this all columns are not showing i have 41 columns and 10 rows but i dont know why this happening here only shows 13 or 14 columns??? but why??
Sad Grad Student
Sad Grad Student 2015년 3월 24일
I need to know what exactly it is that you're doing. Post your code here in the comments so I can answer why you're getting unexpected number of columns.

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

추가 답변 (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