Closest value between two vectors
조회 수: 8 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!