MATLAB Answers

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

1 view (last 30 days)
Sadiq Akbar
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

  3 Comments

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH on 16 Nov 2019
Could you place your out1 with 5 estimated vectors, and also the rearrangement you want to obtain?
Sadiq Akbar
Sadiq Akbar on 16 Nov 2019
0.1762 1.2324 1.0635 0.6634 1.5758 1.3850
1.0593 1.6174 0.0352 1.3940 1.5638 0
0.9919 1.1521 0 1.3907 1.5709 1.7159
1.3609 0.3294 1.3110 1.3999 0.6626 1.5676
0.9249 1.7697 0.1672 1.4018 1.5666 0.3580
1.1263 1.4306 0.2976 1.3907 1.5704 0.7058
and so on.
Now if we see the 1st element (0.1762) in 1st vector is estimated for u(1), 2nd element (1.2324) for u(3) and 3rd element (1.0635) for u(2). This means elements 2 and 3 are interchanged in 1st vector. I re-arrange it manually like this [0.1762 1.0635 1.2324 0.6634 1.3850 1.5758]. As you can see the same change occurs for the last three elements also.
Likewise 1st element (1.0593) in 2nd vector is estimated for u(2), 2nd element (1.6174) for u(3) and 3rd element for u(1). This means here position of u(1) has shifted to 3rd position, position of u(2) has been shifted to 1st position and position of u(3) has been shifted to 2nd position in 2nd estimated vector. Sometimes this chnage takes place and sometimes doesn't. But if the chnage takes place, it is like this:
position 1 and 2 are interchanged with each other
position 1 and 3 are interchanged with each other
position 2 and 3 are interchanged with each other
position 1 goes to position 3 and position 2 goes to position 1 and position 3 goes to position 2
I cann't see these vectors during running the program, but they are diaplayed at the end of execution. I want that during running, Matlab should re-arrange them each on the same pattern as that of u.i.e. estimated value for u(1) i.e. 0.5 should come 1st, estimated value for u(2) i.e.1 should come 2nd and estimated value for u(3) should come 3rd in estimatec vector. Whatever chnage occurs in 1st three elements in an estimated vector, the same change happens in the last three elements of estimated vector also.
Regards,
the cyclist
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.

Sign in to comment.

Answers (0)