MATLAB: How to re-arrange swapped elements in an estimated vector as compared to a reference vector

re-arranging vector elementsre-order elements of estimated vector in comparison with a reference vectorvector comparison

I have a reference vector u=[0.5 1 1.5 0.6981 1.3962 1.5707].
I estimate several vectors of the same size using a metaheuristic algorithm. But the problem is that sometimes the algorithm gives me vectors in which the positions of elements are swapped and sometimes there is no change in the positions i.e. sometimes positios of 1st and 2nd elements are interchanged, sometimes positions of 1st and 3rd elemtns are interchnaged and sometimes positions of 2nd and 3rd eleents are interchanged with each other in the estimated vectors.And if the changes ocuurs in the positions of two elements in the 1st three elements, the same change occurs in the last three elements also.But sometimes the positions are not interchnaged in the estimated vectors. I want to re-arrange the estimated vector elements if they interchange their positions. What check should I keep on the positions of the elements of the estimated vectors. Regards,

Best Answer

Here is a different approach. It relies on the fact that you want the first three inputs to be in ascending numeric order, and ignores whether they are close to [0.5, 1.0, 1.5].
% Define indices for convenience
half_1st = 1:3;
half_2nd = 4:6;
% Reference vector
u=[0.5 1 1.5 0.6981 1.3962 1.5707];
% Some of the inputs
temp = [ ...
1.4293 1.6429 0.1220 1.3833 1.5643 0.7691
1.0385 0.5639 0.5502 1.5833 1.4221 1.5648
0.6842 1.4170 0.1005 1.3988 1.5756 0.0398
0.0156 1.3050 0.9022 0 1.5700 1.3977
0.7765 0.0962 1.3453 1.4011 0.7075 1.5688
1.2783 0.0559 1.1249 1.5817 0.4605 1.3814];
% Loop over the sample inputs
for nn = 1:6
[sortTemp, sortIndex] = sort(temp(nn,half_1st));
two(nn,:) = temp(nn,[half_1st(sortIndex) half_2nd(sortIndex)]);
end
I hope this one works for you.