how to get a new array with some values ​​from an old array

38 ビュー (過去 30 日間)
Santo Lino
Santo Lino 2022 年 11 月 26 日
コメント済み: Voss 2022 年 11 月 27 日
Hi, I'm a matlab beginner, I have a little problem with creating a new matrix. I have a matrix "A" of dimensions (1840x44), I am attaching a photo of a part of it.
I need to get a matrix "B" that has the values ​​of matrix "A" that are greater than 20. I tried doing B = (A>=20), but it only returns the positions (I need the value). Many thanks to whoever can help me with this little problem.

採用された回答

Voss
Voss 2022 年 11 月 26 日
B = (A >= 20) % same as B = A >= 20, i.e., without parentheses
makes B a matrix of logicals, the same size as A; the elements of B where A is greater than or equal to 20 are true; other elements of B are false.
To get the values of A that are >= 20, you can say:
B = A(A >= 20)
but it won't be a matrix, it will be a column vector. The elements of B will be in the same (column-major) order as they are in A.
Example:
A = [1 3.2 5.4]+[0;1;2;3]
A = 4×3
1.0000 3.2000 5.4000 2.0000 4.2000 6.4000 3.0000 5.2000 7.4000 4.0000 6.2000 8.4000
B = A(A >= 4)
B = 8×1
4.0000 4.2000 5.2000 6.2000 5.4000 6.4000 7.4000 8.4000
Alternatively, if you want B to be a matrix the same size as A, containing the elements of A that are >= 20 located at the same locations where they are in A and say zeros or NaNs everywhere else, you can do something like this:
B = NaN(size(A)); % or B = zeros(size(A)); % for instance
idx = A >= 4;
B(idx) = A(idx)
B = 4×3
NaN NaN 5.4000 NaN 4.2000 6.4000 NaN 5.2000 7.4000 4.0000 6.2000 8.4000
  2 件のコメント
Santo Lino
Santo Lino 2022 年 11 月 27 日
thank you very much for helping :)
Voss
Voss 2022 年 11 月 27 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

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