# Replace the empty output of find() by the closest element

2 ビュー (過去 30 日間)
Asatur Khurshudyan 2020 年 1 月 24 日
コメント済み: Asatur Khurshudyan 2020 年 1 月 26 日
Hello everyone,
I have two arrays
a1 = [t1, t2, t3, t4, t5];
a2 = [t1, t1 + d, t1 + 2 * d, t1 + 3 * d];
I use find()
for i = 1:length(a1)
indices(i) = find( a1 == a2(i) );
end
If some elements of a2 are not contained in a1, some of the find() outputs are empty. How to replace these empty elemenets by the indices of the next closest number?
I have tried
for i = 1:length(a1)
indices(i) = find( a1 == a2(i) );
if isempty(indices(i))
indices(i) = indices(i + 1);
end
end
but apparently it does not work.

#### 0 件のコメント

サインイン to comment.

### 採用された回答

John D'Errico 2020 年 1 月 24 日

You are trying to use the wrong tool (i.e., find) to solve this. Well, you could use find, I suppose, with some significant effort. But instead, just use a tool designed to solve that problem.
You apparently want to locate the index of the closest element in a2 to each element in a1. If there is an exact match, that is a great thing, but otherwise, you want the closest match. I'm not sure how you would resolve the case where there are two elements of a2 that are equidistant. I would think that logically, you would choose the first such match from equals.
Regardless, This is a problem solved by knnsearch.
a1 = [2, 3, 4, 5];
a2 = [1, 3, 5, 7, 9, 11];
[idx,D] = knnsearch(a2',a1')
idx =
1
2
2
3
D =
1
0
1
0
As you can see, knnsearch did exactly that.

#### 6 件のコメント

John D'Errico 2020 年 1 月 25 日
I might blame it on the name - find. You want to find something? Use find. What are the odds someone would think to look for a name like knnsearch? This is why lookfor can be such a useful utility, since if the writers of a tool were good at the task of writing an H1 line, then you will find knnsearch by a careful choice of keywords. I'm running MATLAB flat out right now on a problem, so I will not test this claim, but I'd bet that
lookfor distance
might pop knnsearch up as one of the options. As it turns out, distance did not find knnsearch, but
lookfor nearest
will do so well enough.
Another important idea is something that I often like to do - just just help on a toolbox directory. For example, if you have the symbolic toolbox, then
help symbolic
will give you a long list of functions, with one line descriptions of what they do. A great way to learn what tools exist is to do exactly that sort of thing. Read the H1 lines shown, then look at the help for any function that happens to catch your eye as something of interest.
John D'Errico 2020 年 1 月 25 日
Oh, by the way, interp1 would also have worked for this problem, though there is no reason why someone would have thought to use it.
That is, to find the index of the closest point in a2, for every point in a1?
ind = interp1(a2,1:numel(a2),a1,'nearest');
That should work. As long as the elements of a2 are sorted in an increasing order, and there are no replicates in a2, no problem.
Should you use interp1 for that? Well, it is a bit of a hack, using a tool to solve a problem it is not designed to solve, but it just happens to do what you want anyway.
Better is to just use knnsearch, a tool designed for the purpose.
Asatur Khurshudyan 2020 年 1 月 26 日
Thank you for your comment. I will test it, because knnsearch works very slowly.

サインイン to comment.

### その他の回答 (0 件)

サインイン してこの質問に回答します。

Translated by