# How to arrange a vector elements on the pattern of a reference vector

4 views (last 30 days)
Sadiq Akbar on 16 Nov 2019
Commented: the cyclist on 16 Nov 2019
I have a reference vector u=[0.5 1 1.5 0.6981 1.3962 1.5707].
I want to estimate this vector via an algorithm. For that I am using an Algorithm "Flower Pollination" which returns me a vector of the same size as u. This returned vector is my estimated vector. I run this algorithm 100 times and each time I get an estimated vector of the same size as u. For this iteration, I have set up a for loop like this:
for n=100:199
[out1,out2,out3]=flower(n,Remaining input arguments---)
temp(nn,:)=out1;
------------
-----------
%-------------Check for swapping-----------------
if abs((temp(nn,1)-u(2)))< abs((temp(nn,1)-u(1)))
two(nn,:)=[temp(nn,2) temp(nn,1) temp(nn,3) temp(nn,5) temp(nn,4) temp(nn,6)];
elseif abs((temp(nn,1)-u(3)))< abs((temp(nn,1)-u(1)))
two(nn,:)=[temp(nn,3) temp(nn,2) temp(nn,1) temp(nn,6) temp(nn,5) temp(nn,4)];
elseif abs((temp(nn,2)-u(3)))< abs((temp(nn,1)-u(1)))
two(nn,:)=[temp(nn,1) temp(nn,3) temp(nn,2) temp(nn,4) temp(nn,6) temp(nn,5)];
else
two(nn,:)=temp(nn,:);
end
end
The output "out1" is a vector returned by the algorithm which is not exactly equal to u but is nearly equal to u. i.e. each element of out1
is nearly equal to each element of u.But when I run this code, and check the 100 estimated vectors. In some of these vectors, positions of estimated elements have been interchanged and in some of these vectors there is no position interchange.If the change ocuurs, its like this:
Positions 1 and 2 interchange with each other
Positions 1 and 3 interchange with each other
Positions 2 and 3 interchange with each other
Then I manually re-arrrange those estimated vectors.I want that Matlab should check each of them in advance and re-arrange them automatically before displaying them.
Regards
the cyclist on 16 Nov 2019
Before anyone tries to work on this question, I suggest you look at the very long thread of questions, comments, and attempted solutions for the same problem, posted here.

### Categories

Find more on Spatial Search 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!

Translated by