Closest value between two vectors

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일
you want to compare every element in k with k and l? if so, the next element in k and the current element in l? for the above example of yours, what should be the output?
numnum
numnum 2017년 11월 21일
Yes. compare very k element with k and l and find zhere the value closest and superior is, in k or l. The output should be for k(1)= 1, closest value > is l(1)=5. Countl=0+1 for k(2)= 9, closest value > is k(3)=12. Countk=0+1 for k(3)= 12, closest value > is l(3)=32. Countl=1+ 1 for k(4)= 203, closest value > is l(4)=1003. Countl=2+1
KL
KL 2017년 11월 21일
I do not understand when you say ... closest value > is...
What does it mean?
...for k(2)= 9, closest value > is k(3)=12...
l(2) is 8, which is closer to 9 than 12.
and what happens to the last value of k? would you only compare it with l's last value (since there's no next element in k)?
numnum
numnum 2017년 11월 21일
the closest value that is superior. 8 < 9 so it would be rejected
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일

1 개 추천

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

numnum
numnum 2017년 11월 21일
Thanks ! But k is a vector, and l is a seperate vector, and i want to co;pare the two together.
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.

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

질문:

2017년 11월 21일

편집:

2017년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by