How can one extract a column based on a value of another column?

2 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2020 年 8 月 15 日
コメント済み: Les Beckham 2020 年 8 月 18 日
I have a two column matrix A. The first column takes a value of 0 or 1. I want to extract the elements of the second column according to the value of the first column. For example,
A = [ 0 1; 1 0; 0 2];
Then I want to create
A_0 = [1; 2];
A_1 = [0];
Please advise.

回答 (1 件)

Les Beckham
Les Beckham 2020 年 8 月 15 日
>> A = [ 0 1; 1 0; 0 2];
>> A_0 = A(A(:,1)==0,2)
A_0 =
1
2
>> A_1 = A(A(:,1)==1,2)
A_1 =
0
  1 件のコメント
Les Beckham
Les Beckham 2020 年 8 月 18 日
If this answered your question, you should accept the answer.
If not, you should explain why it did not answer your question. Usually, this requires clarifying your question.
If you continue to ignore the answers to your questions, those of us who have been answering them will begin to ignore your questions.
Have a nice day.

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by