# Find the key for vector transformation

4 views (last 30 days)
Ali on 27 Sep 2022
Commented: Ali on 27 Nov 2022 at 22:36
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);
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Walter Roberson on 27 Sep 2022
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 CommentsShowHide 4 older comments
Ali on 28 Sep 2022
Thank you so much, I will try ga.

Sign in to comment.

### More Answers (1)

Matt J on 28 Sep 2022
See Example 2: Optimal Reordering of Points in the Examples tab of
##### 1 CommentShowHide None
Ali on 27 Nov 2022 at 22:36
Thank you for late reply. I saw your comment now, I appreciate and take a look.

Sign in to comment.

### Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!