Iterating through each element in a matrix

16 ビュー (過去 30 日間)
Sai Gudlur
Sai Gudlur 2020 年 5 月 22 日
コメント済み: Sai Gudlur 2020 年 5 月 22 日
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 日
s = intersect(A, B);
  5 件のコメント
Sai Gudlur
Sai Gudlur 2020 年 5 月 22 日
Thanks for help.
Sai Gudlur
Sai Gudlur 2020 年 5 月 22 日
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 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by