How to check if any component of a vector/matrix is matching with any of the component of another vector?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
SALMAN KHAN
am 5 Mär. 2023
Kommentiert: SALMAN KHAN
am 5 Mär. 2023
% Let say I have two vector A and B,
A = [2 3 4 1 4 9 10];
B = [2 9];
if ismember(A,B)
C = 2*A;
else
C = 0*A;
end
It is giving me zero vector but it should give me 2A. I know that I can use any(ismember(A,B) to get desired result but let say A is matrix (instead of vector) and I want to check if any of the component in A matrix is matching with any component of B vector then "If" condition should satisfy. I dont want to use any(any(...)). Is there any direct way to do that? Thanks.
0 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 5 Mär. 2023
%modified A to be a matrix
A = [2 3 4; 1 4 9; 10 5 6];
B = [2 9];
%You can use intersect to find if there are any
%common elements
any(intersect(B,A))
%or change the order of the input
any(ismember(B,A))
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!