Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Beibit Sautbek
Beibit Sautbek am 23 Jul. 2016
Beantwortet: Star Strider am 24 Jul. 2016
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?

Antworten (1)

Star Strider
Star Strider am 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

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by