How to pick the elements of matrix only for selected value?

22 ビュー (過去 30 日間)
Anisa
Anisa 2013 年 2 月 6 日
I have a matrix named B with size 128x128. The matrix has elements with different value in range from 2-196. I want to use the elements with value only 2-36 and arrange it into a row.
For example:
B = [14 7 89 157 190 45 26; 144 11 9 20 78 23 123; 1 13 23 8 100 88 31; 6 30 33 145 166 29 15]
I want to pick the value only in range 2-36, and rearrange it into a row.
E = [ 14 7 26 11 9 20 23 13 23 8 31 6 30 33 19 15]
How to do this?

採用された回答

Jan
Jan 2013 年 2 月 6 日
E = reshape(B(B>2 & B<36), 1, []);

その他の回答 (1 件)

Hossein
Hossein 2013 年 2 月 6 日
編集済み: Hossein 2013 年 2 月 6 日
First, I'd convert it to a column vector:
BC = B(:);
Then, I'd use find() to search in values:
E = BC(find(BC>=2).*find(BC<=36));
You can also use find() on a matrix directly, which I leave it to you to discover, type:
Help find
  1 件のコメント
Jan
Jan 2013 年 2 月 6 日
編集済み: Jan 2013 年 2 月 6 日
find(BC >= 2) .* find(BC <= 36) fails, when the number of elements differ. Using logical indexing is more efficient, such that you can omit the find. And then the ".*" becomes equivalent to the and() operation:
E = BC(BC >= 2 & BC <= 36);

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by