Search subarray closest other array

조회 수: 3 (최근 30일)
galaxy
galaxy 2019년 12월 3일
댓글: galaxy 2019년 12월 4일
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
galaxy
galaxy 2019년 12월 4일
편집: galaxy 2019년 12월 4일
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]

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

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 12월 3일
편집: KALYAN ACHARJYA 2019년 12월 3일
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
galaxy
galaxy 2019년 12월 4일
I need to change some thing, but it is OK
Thank you so much.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Array Geometries and Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by