Help splitting a matrix based on a specified column.

1 回表示 (過去 30 日間)
Worravit nova
Worravit nova 2011 年 1 月 22 日
コメント済み: Stephen23 2020 年 8 月 18 日
I have matrix(A) with 13x3 like this:
No. value1 value2
1 12 0
2 4 0
3 8 0
4 1 0
5 9 0
6 5 0
7 3 0
8 1 0
9 1 2
10 12 0
11 11 12
12 10 0
13 3 4
And I want to split it to two matrix B and C like this:
B=
No. value1 value2
1 12 0
2 4 0
3 8 0
4 1 0
5 9 0
6 5 0
7 3 0
8 1 0
10 12 0
12 10 0
which is only colum of value2=0
And matrix C=
No. value1 value2
9 1 2
11 11 12
13 3 4
which is only colum of values2 not zeros
I did :
nonzeros(A)
but it gave me a one colum matrix which is not I want. I need to keep up the relation between value1 and value2.
find(shearingedges(:,1)>0) & (shearingedges(:,2)>0):
But it gave me a colum matrix with 1 and 0.
Could anyone help please?
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 1 月 22 日
Unfortunately the formatting of this question makes it difficult to figure out what you are asking. It would help to edit the question so that the different rows of the examples were on different lines.

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

採用された回答

Paulo Silva
Paulo Silva 2011 年 1 月 22 日
A=[1 12 0 2 4 0 3 8 0 4 1 0 5 9 0 6 5 0 7 3 0 8 1 0 9 1 2 10 12 0 11 11 12 12 10 0 13 3 4]
B=A(find(A==0))
C=A(find(A))
  3 件のコメント
Todd Flanagan
Todd Flanagan 2011 年 1 月 25 日
Worravit says, "Thank you so much paulo and Rob
your answers very helpfull."
Paulo Silva
Paulo Silva 2011 年 1 月 26 日
Sean I know that find wasn't required, I use find often just for the code to be more readable by new users.

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

その他の回答 (2 件)

Rob Graessle
Rob Graessle 2011 年 1 月 22 日
Try this:
B=A(A(:,3) == 0, :)
C=A(A(:,3) ~= 0, :)

Hy
Hy 2011 年 1 月 25 日
There seem to be 3 questions here.
1. How to select rows from a matrix according to a criterion?
2. How to select rows from a matrix according to two criteria?
3. What does nonzeros do?
Answers:
1. Rob's answer using logical indexing ( docs, newsletter ) is correct:
myCriteria = A(:,3) == 0;
B = A(myCriteria,:);
C = A(not(myCriteria));
2. The answer-seeker then poses the code
find(shearingedges(:,1)>0) & (shearingedges(:,2)>0):
if we remove the find(), a parenthesis, and end the line with a semicolon, we get
myCriteria = shearingedges(:,1)>0) & shearingedges(:,2)>0;
This is valid syntax for logical indexing, and the rest of the syntax from question 1 can be used. The "column matrix with 1 and 0" is the vector used for logical indexing. MATLAB treats a 1 as equivalent to true and a 0 as equivalent to false, and so uses them when displaying logical arrays.
3. The link above gives a full explanation, but simply, nonzeros is not directly helpful for indexing because it returns only the values of an array that are nonzero.

カテゴリ

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