Delete the row of matrix

1 回表示 (過去 30 日間)
Laura
Laura 2015 年 2 月 9 日
コメント済み: Guillaume 2015 年 2 月 9 日
I want to delete multiple rows of matrix.
For example,
Matrix A contains X in the first column, and Y in the second column. Note that X and Y does not have to be a whole number .
A = [ 1.122 2.111; 1.122 2.3, etc,....]
If X values are in the interval of [1.12 -1.125] and Y values are in the interval of [2.12-2.3], then delete this row.If X is in the range but Y is not, then dont delete them. Note that I have to search the matrix A to find whether X and Y in the interval that I defined, and then delete them. From the example above, the second row needs to delete by looking at it. But I need to search them for the huge matrix which I cant use eye to find them. I need you guys helps.
Thanks.
  2 件のコメント
Hikaru
Hikaru 2015 年 2 月 9 日
編集済み: Hikaru 2015 年 2 月 9 日
Is this homework?
If it is, I'll give you a hint on where to start. Say you have a vector b and the condition c to determine which element will be deleted.
b = [1 2 3 0];
c = b>=3;
b(c)=[] % this will delete that element
Laura
Laura 2015 年 2 月 9 日
This is not a homework. Thanks though.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2015 年 2 月 9 日
It's fairly basic matlab matrix manipulation. Use logical indexing:
A(A(:, 1) >= 1.12 & A(:, 1) <= -1.125 & A(:, 2) >= 2.12 & A(:, 2) <= -2.3, :) = []
% A(:, 1) is your X, A(:, 2) is your Y
% A(:, 1) >= 1.12 & A(:, 1) <= -1.125 is all the rows of A for X in [1.12 -1.125] (1)
% A(:, 2) >= 2.12 & A(:, 2) <= -2.3 is all the rows of A for Y in [2.12 -2.3] (2)
% (1) & (2) is X in [1.12 -1.125] AND Y in [2.12 -2.3]
% A(condition, :) = [] remove all the rows of A that fulfill the condition
  2 件のコメント
Laura
Laura 2015 年 2 月 9 日
Thanks so much Guillaume. It works fine. However, I want to ask why it does not work if I try this:
rx=find(A(:, 1) >= 1.12 & A(:, 1) <= -1.125);
ry=find(A(:, 2) >= 2.12 & A(:, 2) <= -2.3);
A(rx,ry)=[];
it gave me an error as "Subscripted assignment dimension mismatch".
Thanks again.
Guillaume
Guillaume 2015 年 2 月 9 日
You seem to be a bit confused about array indexing. In
A(rx, ry)
ry is the columns of A. You've only got two columns.
If you wanted to use your method, you'd have to do:
rx=find(A(:, 1) >= 1.12 & A(:, 1) <= -1.125);
ry=find(A(:, 2) >= 2.12 & A(:, 2) <= -2.3);
A(intersect(rx, ry), :) = [];
It's a lot more work though than:
logicalx = A(:, 1) >= 1.12 & A(:, 1) <= -1.125;
logicaly = A(:, 2) >= 2.12 & A(:, 2) <= -2.3;
A(logicalx & logicaly, :) = [];

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by