Finding a value of one vector based on the nonzero values of another vector

3 ビュー (過去 30 日間)
L'O.G.
L'O.G. 2022 年 12 月 8 日
コメント済み: Stephen23 2022 年 12 月 8 日
Given two vectors, how do you determine the value of A where B is 1? In this case, A=[5 4]. How do I do this?
A = [2 5 1 4];
B = [0 1 0 1];

回答 (3 件)

Arif Hoq
Arif Hoq 2022 年 12 月 8 日
編集済み: Arif Hoq 2022 年 12 月 8 日
A = [2 5 1 4];
B = [0 1 0 1];
[I C]=find(B==1);
output=A(C)
output = 1×2
5 4

Image Analyst
Image Analyst 2022 年 12 月 8 日
If B is always guaranteed to be 0 or 1, you can do this
A = [2 5 1 4];
B = [0 1 0 1]; % B is a double here.
A = A(logical(B)) % Need to cast B to logical
A = 1×2
5 4
If B can be anything and you want to get A where B is not equal to zero:
A = [2 5 1 4];
B = [0 23 0 341];
A = A(B ~= 0)
A = 1×2
5 4
  1 件のコメント
Stephen23
Stephen23 2022 年 12 月 8 日
Using LOGICAL() works in both cases:
A = [2 5 1 4];
B = [0 23 0 341];
A = A(logical(B))
A = 1×2
5 4

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


Voss
Voss 2022 年 12 月 8 日
A = A(B == 1)

カテゴリ

Help Center および File ExchangeCreate Block Masks についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by