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

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

Answers (1)

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 Comments

At least an example of the arrays you’re starting with and the result you want could be helpful.
I still don’t understand exactly what you want. Your last Comment suggests that the unique (or uniquetol) functions could work.
I think ismember might be useful... Haven't used MATLAB for long so reading about the functions you've suggested :)
So here's an example, array:
1 3
5 2
5 8
0 0
So, I would look at 3, and say "what value is closest to 3 in the previous column?" The answer is 1. So in the new array, the first column would be the same, and the first row of second column would remain as 3. I would then go to number 2 and ask "which value in the previous column is closest to 2?" The answer is 1 BUT, 1 has already been matched with 3. So I can't have 1 because it's "taken" so it would look at number 5 and take that instead. But there are two 5's in the first column so this causes problems too because you don't want to take the first 5 if it's already taken.
This process repeats for the whole array which is about size 20 X 8000
OK, so what do you do next? Choose 0 because it’s closest to 2, just stop, or choose something else (and with what criteria)?
The ismember functions works best for integers. If you have floating point numbers (or ‘integers’ that were calculated from floating-point numbers), rounding them to integers or using ismembertol work best.
All my values are actually complex numbers with like 4 decimal places but it shouldn't affect the ordering process at all because I was hoping I could say, "if that index from the previous column has been taken, choose the second BestMatch". So yeah, it would pick 0 as it's not taken yet.
If the second BestMatch has been taken, choose the third BestMatch etc. The criteria is that I'm trying to find the value that's closest but some values are the same so I don't want something to pick the same thing. So, I guess in my code, I'm mainly trying to say "if BestMatch = k has been taken before, BestMatch ~= k (whatever k is at that iteration) and find the next closest index from previous column. But when it actually checks values, it goes through each row from top to bottom over again each time, but doesn't exempt the value since it doesn't have memory of having chosen something before. (This is confusing I guess..)
OK. You have complex numbers as indices? How does that work? How do you define ‘distance’?
Pardon me, but the more I learn about what you want to do, the less I understand.
Actually, there's a much much simpler way to do what I want to do! But thanks for all your help nonetheless :)
(complex numbers are the values in the arrays, not the indices)
I understand distance with respect to the integer indices. My question was about distance with respect to the complex numbers.
You can also use the setdiff function to eliminate indices you’ve already chosen, returning only those that are still ‘eligible’. You have to keep track of those you’ve already chosen in a vector to use them with the setdiff function. That might make choosing the others easier.
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
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.

Sign in to comment.

Asked:

on 14 Jan 2017

Edited:

on 14 Jan 2017

Community Treasure Hunt

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

Start Hunting!