Find the key for vector transformation

Hello friends, Below is the optimization work. Even though I woked with fminsearch and similar tools, I cannot figure this out. Any help? Thank you very much. A & B & KEY are vectors of same length. the KEY is unknown. find the KEY such that minimize the function B-A(KEY).
clc;clear
A= [25 21.2 0.4 3.5 15 60 14 8.1 16.9 1.1];
KEY= [3 5 8 1 10 9 7 6 2 4];
B= A(KEY) + rand(1,10);

 Accepted Answer

You do not want to minimize B-A(KEY): those are vectors and you cannot minimize a vector.
A= [25 21.2 0.4 3.5 15 60 14 8.1 16.9 1.1];
KEY= [3 5 8 1 10 9 7 6 2 4];
B= A(KEY) + rand(1,10);
Kp = perms(KEY);
values = sum((B - A(Kp)).^2,2);
[bestresult, idx] = min(values);
bestresult
bestresult = 3.4348
Kp(idx,:)
ans = 1×10
3 5 8 1 10 9 7 6 2 4

5 Comments

Thank you so much dear Walter, The code above is actually the simplified version of the main code where the key has the length of 44, I do not think my laptop can run perms for such huge data.
Do you need the absolute best match, or would a "reasonably good" match be useful?
If you only needed a "reasonably good" match then you could use ga() with custom creation and integer constraints that enforced that the trial values were an integer permutation. But ga() often does not find the global minima -- for example in a Traveling Salesman problem it might get stuck with two nodes exchanged when the difference in total distance between the two is not large.
Ali
Ali on 27 Sep 2022
Edited: Walter Roberson on 28 Sep 2022
The code is actually much complicated than that. The answer KEY is unique, it is not mathematically possible of having more than one answer, the problem I faced is how to define the key with initial values in optimization tools? In other words, we tell the optimization to converge between upper and lower bounds but it has to understand the integers are unique, if x(2) for example is 3, no other x can have this value.
You have phrased this as a minimization, not as a key recovery. minimization implies that potentially after the noise has been added, that a different key than the initial key is now the best one.
That's why I was talking about custom functions to enforce integer permutation.
Thank you so much, I will try ga.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 28 Sep 2022

1 Comment

Thank you for late reply. I saw your comment now, I appreciate and take a look.

Sign in to comment.

Categories

Asked:

Ali
on 27 Sep 2022

Commented:

Ali
on 27 Nov 2022

Community Treasure Hunt

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

Start Hunting!