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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by