MATLAB Answers

Search subarray closest other array

2 views (last 30 days)
galaxy
galaxy on 3 Dec 2019
Commented: galaxy on 4 Dec 2019
Dear all
I want to search subarray closest other array as following.
A = [4, 5.5, 7, 8.1, 1, 5, 6, 7, 8, 9];
B = [5.5, 6.2, 7, 7.9]
The expected output is sub-array of A closest B which is [5, 6, 7, 8]
Do anyone have ideas?
Thank you so much

  1 Comment

galaxy
galaxy on 4 Dec 2019
I need to search closest sub-array, not element of array.
A = [4, 5.5, 7, 8.1, 1, 5, 6, 7, 8, 9];
% | | | | closest
B = [5.5, 6.2, 7, 7.9]

Sign in to comment.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 3 Dec 2019
Edited: KALYAN ACHARJYA on 3 Dec 2019
One way:
A = [4, 5.5, 7, 8.1, 1, 5, 6, 7, 8, 9];
B = [5.5, 6.2, 7, 7.9];
for i=1:length(A)-4
result(i)=sum(A(i:i+3)-B);
end
idx=find(result==min(result));
result_array=A(idx:idx+3)
You can use simmilar logic to implement it without loop too.

  1 Comment

galaxy
galaxy on 4 Dec 2019
I need to change some thing, but it is OK
Thank you so much.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Translated by