How to remove some rows from the matrix according to conditions?

조회 수: 4 (최근 30일)
Beibit Sautbek
Beibit Sautbek 2016년 7월 23일
답변: Star Strider 2016년 7월 24일
I have two matrices: the main matrix is:
u =
0 0 0 5
0 0 4 0
0 0 4 5
0 3 0 0
0 3 0 5
0 3 4 0
0 3 4 5
2 0 0 0
2 0 0 5
2 0 4 0
2 0 4 5
2 3 0 0
2 3 0 5
2 3 4 0
2 3 4 5
the matrix which gives condition to the matrix u is matrix:
p =
3
4
here, numbers 3 and 4 means two conditions in order to remove rows from matrix u:
So, what I need to find is :
1) Find that rows where after number 3, also there are zeros: In this case, it is rows:
0 3 0 0
2 3 0 0 .
Remove this two rows from the matrix u.
2) Find that rows where just zeros until 4. In this case:
0 0 4 0
0 0 4 5
And delete this rows from the matrix u.
My matrix should be like : u =
0 0 0 5
0 3 0 5
0 3 4 0
0 3 4 5
2 0 0 0
2 0 0 5
2 0 4 0
2 0 4 5
2 3 0 5
2 3 4 0
2 3 4 5
Could anyone help me?

답변 (1개)

Star Strider
Star Strider 2016년 7월 24일
One approach:
r3 = all(u(:,2:end) == repmat([3 0 0],size(u,1),1),2);
r4 = all(u(:,1:3) == repmat([0 0 4],size(u,1),1),2);
u = u(~r3 & ~r4,:)
u =
0 0 0 5
0 3 0 5
0 3 4 0
0 3 4 5
2 0 0 0
2 0 0 5
2 0 4 0
2 0 4 5
2 3 0 5
2 3 4 0
2 3 4 5

카테고리

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