フィルターのクリア

How to exempt an index of an array from a loop after each iteration?

3 ビュー (過去 30 日間)
belle
belle 2017 年 1 月 14 日
編集済み: belle 2017 年 1 月 14 日
I have two arrays called array and newarray. Both are of size N X sz. The first column of array is the first column of newarray. I have a loop that goes through each value of array looking at each row of each column (2nd column onwards), and it finds the value (index) from the previous column which is closest. The code is below. However, some of the values in the columns are the same and I do not want an index from the previous column to be matched more than once. How do I exempt an index from being chosen again if it already has a match from the previous iteration?
newarray(1:N,1) = array(1:N,1);
for j = 1:1:sz-1
for s = 1:1:N
BestDistance = 10;
A = newarray(s,j);
for k = 1:1:N
B = array(k,j+1);
Distance = abs(B-A);
if Distance < BestDistance
BestDistance = Distance;
BestMatch = k ;
%If the index k has already been matched previously, I want it to be exempt from being chosen again
end
end
newarray(s,j+1)=array(BestMatch,j+1);
end
end

回答 (1 件)

Star Strider
Star Strider 2017 年 1 月 14 日
Without actually having both ‘array’ and ‘newarray’, it’s not possible to write specific code.
See if the find, ismember (or ismembertol) functions will do what you want.
  10 件のコメント
belle
belle 2017 年 1 月 14 日
編集済み: belle 2017 年 1 月 14 日
No no no!! The distance has nothing to do with the indices. The distance is the distance calculated between the complex numbers (i.e. values in the array). Distance plays no part with respect to the indices.
The easier way to do it (now I have the solution) is to prefill the newarray with a very improbable number that doesn't appear in array (e.g. 12345). And within the loop, I just say something like if newarray(BestMatch,j+1)=12345, then BestMatch =k. Otherwise BestMatch ~=k. :) But setdiff function does also look useful too :) Thanks for all your suggestions! Have a good weekend
Star Strider
Star Strider 2017 年 1 月 14 日
My pleasure.
Now I’m completely confused. I have no idea how the distances and indices interact.
I’m happy you got this sorted. You have a good weekend, too.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by