Hi everyone
Now I have a matrix which has two columns and several rows.
How can I remove the rows which contains value that read 0 and create a new matrix?
Any help would be really appreciated!

 채택된 답변

Star Strider
Star Strider 2015년 4월 23일

1 개 추천

The find function with two outputs is helpful here:
M = randi([0 10], 20, 2); % Create Data
[r,~] = find(M == 0); % Rows With ‘0’
M(r,:) = []; % Delete Rows With ‘0’

추가 답변 (1개)

Stephen23
Stephen23 2015년 4월 23일
편집: Stephen23 2015년 4월 23일

1 개 추천

We can do this simply using all and logical indexing, which is faster than using find and also allows us to allocate directly to a new matrix:
>> M = randi([0,5], 8, 2)
M =
2 4
5 4
4 4
5 2
3 3
0 1
5 4
5 0
>> N = M(all(M~=0,2),:)
N =
2 4
5 4
4 4
5 2
3 3
5 4

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2015년 4월 23일

댓글:

2015년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by