Finding a value of one vector based on the nonzero values of another vector
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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];
0 Kommentare
Antworten (3)
Image Analyst
am 8 Dez. 2022
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
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)
1 Kommentar
Stephen23
am 8 Dez. 2022
Using LOGICAL() works in both cases:
A = [2 5 1 4];
B = [0 23 0 341];
A = A(logical(B))
Siehe auch
Kategorien
Mehr zu Author Block Masks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!