フィルターのクリア

find common elements of two arrays

1 回表示 (過去 30 日間)
Saad
Saad 2012 年 10 月 30 日
Dear all,
I have two arrays of different size say A(nx1) and B(mx1) where n differs from m. I would like to find the elements of B that are as close as possible to the element of A and display them. How can I do that please? Most of matlab applications I found (equal, ismember, find, etc) they require arrays of the same size which is not my case. Thank you very much for any guidance or advice you could give me
Regards
S

回答 (3 件)

Chris A
Chris A 2012 年 10 月 30 日
a=[5; 7; 3; 1; 8; 6; 2];
b=[3; 9; 0];
d=repmat(a,1,numel(b)) - repmat(b', numel(a),1);
[~,i]=min(abs(d));
horzcat(b, a(i)), % Closest elements to b

Matt J
Matt J 2012 年 10 月 30 日
ISMEMBER doesn't require the arguments to be the same size and neither does this.

Daniyal Awan
Daniyal Awan 2012 年 10 月 30 日
Another way. The tolerance is the difference you allow. a must be smaller or equal in length to b. I like circshift, even though for loop can hurt in case of long vectors
function closest(a,b,tolerance)
c=[];
if (length(a)<=length(b))
a=[a nan*ones(1,length(b)-length(a))];
for shift=1:length(a)
c=[c ; b(abs(b'-circshift(a',shift))<tolerance+1)'];
end
else
error('beep..wrong order of input vectors!!')
end
disp(c)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by