LoginSignup
2
0

More than 5 years have passed since last update.

[matlab]Obtain vectors that have a specific number

Last updated at Posted at 2015-06-17

イントロ

行列から、特定の数字を含む行の
全てを取得する方法を調べたので報告します。

How to check if a number is in an array?

事例

下記のような行列から、

     1    10     2     4     5
     ...  ...   ...   ...    ...
     ...  ...   ...   ...    ...
     1    10     2     4     5
     1    10     2     4     5
     3    11    14     0     9
     ...  ...   ...   ...    ...
     ...  ...   ...   ...    ...
     ...  ...   ...   ...    ...
     3    11    14     0     9
     3    11    14     0     9

下記のようなものだけ取得したい。

     3    11    14     0     9
     ...  ...   ...   ...    ...
     ...  ...   ...   ...    ...
     ...  ...   ...   ...    ...
     3    11    14     0     9
     3    11    14     0     9

サンプルコード

grep_matrix.m
a = [1 10 2 4 5];
b = [3 11 14 0 9];
Mat = [repmat(a, 107, 1); repmat(b, 16, 1)];
Mat(any(b(1) == Mat, 2), :);

解説

サンプルコードを走らせるとわかりますが、
事例をみたす実装ができています。
特定の数字を含む行Vectorを探して
1/0を返すany関数を使って、行列について
Mat(any, :)と書くことで目的は達成されました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0