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

4 views (last 30 days)
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?

Answers (1)

Star Strider
Star Strider on 24 Jul 2016
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

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!