フィルターのクリア

How to pull out elements of a matrix based on certain conditions

1 回表示 (過去 30 日間)
Darrian Low
Darrian Low 2018 年 4 月 22 日
編集済み: Darrian Low 2018 年 4 月 22 日
Hi,
I am trying to pull out values from columns 3 and 4 based on what is in column 2. The numbers I am interested in are the values WITH a '1' from column 2. Then I wish to put these values into a ?x1 matrix. I put the '?' there because the length of the matrix can vary with different file inputs, but the width will stay as 1.
This is what the output should look like.
Here is my attempt at coding this problem:
*Photo Removed***
This code partially works, but when it loops, the previous results get over written.
Where 'e' is the length of Matrix_One which in this example is 7. Where Matrix_One is the 7x4 matrix and Matrix_Two SHOULD be the 10x1 matrix.
How should I go about fixing my code?
Thanks! I look forward to reading some answers.

採用された回答

Stephen23
Stephen23 2018 年 4 月 22 日
編集済み: Stephen23 2018 年 4 月 22 日

Where M is your matrix:

reshape(M(M(:,2)==1,3:4).',[],1)

or

tmp = M(M(:,2)==1,3:4).'
tmp(:)
  1 件のコメント
Darrian Low
Darrian Low 2018 年 4 月 22 日
You saved me again mate. Thank you so much for your help!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 4 月 22 日
編集済み: Walter Roberson 2018 年 4 月 22 日
mask = Matrix_One(:,2) == 1;
Matrix_Two = Matrix_One(mask,3:4);
  2 件のコメント
Darrian Low
Darrian Low 2018 年 4 月 22 日
編集済み: Darrian Low 2018 年 4 月 22 日

That's about half way there. The problem is, this is the output:

**Photo Removed**

However I am after this beauty:

**Photo Removed**

Thanks a bunch for your help though. Progress is happening.

Walter Roberson
Walter Roberson 2018 年 4 月 22 日
Matrix_Two = reshape(Matrix_One(mask,3:4) .', [], 1);

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by