Hello,
Below is my code i want check if elements in vector/matrix match the other vector/matrix/scalar and popullate a empty matrix with the common elements. Attaching my code. In the attached code i have two Row Vectors of same size but want to know if it any different if one of them is scalar or (mxn) matrix and other is Row or Column Vector.
A = [1 2 3 2 5 2 5];
B = [3 3 3 3 39 9 9];
s = zeros(size(A));
for i = 1:size(A)
if any(A(i)==B(i))
s = s(i);
end
end

 채택된 답변

Walter Roberson
Walter Roberson 2020년 5월 22일

0 개 추천

s = intersect(A, B);

댓글 수: 5

Sai Gudlur
Sai Gudlur 2020년 5월 22일
intersect tells me how many common elements in both the vectors but does not tell what element is that and location of the elements in each vector.
intersect tells you the value of the common elements, not the number. With your particular data, the only value in common is 3.
Your original question only asks to find the common values, which is what intersect() does.
What is your desired output? A list of common elements, and for each one the row and column indices for each of the two matrices?
Sai Gudlur
Sai Gudlur 2020년 5월 22일
Yes.
Sai Gudlur
Sai Gudlur 2020년 5월 22일
Thanks for help.
Hello Walter,
You had previously answered this question so i looked at the old post and found a solution to what i need.
Thanks for pointing me in the right direction.
Sai
c = find(ismember(a,b,'rows'))
d = [];
for i=1:numel(c)
d(i,:) = a(c(i),:)
end
Option #2
c = intersect(a, b, 'rows', 'stable');

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 5월 22일

댓글:

2020년 5월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by