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 Center および File ExchangeArray Geometries and Analysis についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by