Closest value between two vectors

2 ビュー (過去 30 日間)
numnum
numnum 2017 年 11 月 21 日
編集済み: Andrei Bobrov 2017 年 11 月 21 日
I have two vectors k and l.
k l
1 5
9 8
12 32
203 1003
I want to find for each value in k, if the closest next value is in k or l (if its in k, add + 1 to a counter, if it’s l, add +1 to another counter. Any help would be imesnsly appreciated! Thanks a lot!
  6 件のコメント
KL
KL 2017 年 11 月 21 日
did you read my whole comment?
numnum
numnum 2017 年 11 月 21 日
for the last value of k, z=we don't care about it.

サインインしてコメントする。

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 11 月 21 日
編集済み: Andrei Bobrov 2017 年 11 月 21 日
kl =[1 5
9 8
12 32
203 1003];
z = randi([1 1003],8,1);
[~,ii] = min(abs(kl - reshape(z,1,1,[])),[],2);
out = accumarray(ii(:),1);
for numel(K) ~= numel(L):
K = randi([-45 120],12,1);
L = randi([-15 300],18,1);
z = randi([-45 300], 8 ,1);
[~,ii] = min(abs([K(:);L(:)] - z(:)'));
adds = accumarray((ii(:) > numel(K)) + 1,1);
  3 件のコメント
Image Analyst
Image Analyst 2017 年 11 月 21 日
Then just create kl.
kl = [k, l];
If k and l are different lengths, then please say so. Also say WHY you are wanting to do this.
numnum
numnum 2017 年 11 月 21 日
Yes, but i want to keep information about whether the value comes from k or l. They are of differemt lengths, i should've mentionned that. Each value in an array represents the time at which an event happened. The goal is to find out the number of events that are consequentive in k vs the number of events that happen in k then in l.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by