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

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,

3 Comments

How do you determine you that two values have swapped position?
Expanding on Johannes' question a bit:
You need to explain the exact rule for swapping elements. Give a small, representative example of what the inputs and outputs would be for what you want to do.
Keep in mind that the only thing we know about the problem you are trying to solve is what you tell us here.
I use Flower Pollination Algorithm. My work is to estimate my reference vector.
u=[0.5 1 1.5 0.6981 1.3962 1.5707]
When I run the algorithm, it gives me an estimated vector like
Est1=[0.499 1.002 1.5001 0.6890 1.3880 1.49998]
In this there is no change in the position of any element. But when I run it again, It gives me
Est2=[0.9999 0.5001 1.49999 1.3990 0.6999 1.5880]
As you can see here the position of 1st and 2nd elements are interchanged in Est2 as compared to u. Likewise when I run it again, it gives me
Est3=[0.5001 1.4981 1.0012 0.70001 1.57160 1.39990]
Here the positions of 2nd and 3rd elemnts are interchanged and so on. As is clear when there is no change of positions in two of the the 1st three elements, then no change occurs in the positions of the last three elements also as is clear in Est1. But when the 1st and 2nd elements interchange their positions in the 1st three elements, then the same happens pattern is followed in the last three elements also as in Est2. And the same can be observed in Est3. I want to rearrange these positions on the pattern of my vector u even if the algorithm gives me Est vector having changed positions.
Regards,

Sign in to comment.

 Accepted 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.

1 Comment

Thank you very much the cyclist. Indeed it worked. I am really very thankful to your heartedly help. Regards,

Sign in to comment.

More Answers (2)

% Inputs
u=[0.5 1 1.5 0.6981 1.3962 1.5707];
Est1=[0.499 1.002 1.5001 0.6890 1.3880 1.49998];
Est2=[0.9999 0.5001 1.49999 1.3990 0.6999 1.5880];
Est3=[0.5001 1.4981 1.0012 0.70001 1.57160 1.39990];
% Define a few parameters for convenience
half_1st = 1:3;
half_2nd = 4:6;
tol = 0.01;
% Find the elements of Est1, etc, that are near u
% EDITED for a mistake in the ordering of the ismembertol arguments
[tf1,idx1] = ismembertol(u(half_1st),Est1(half_1st),tol);
[tf2,idx2] = ismembertol(u(half_1st),Est2(half_1st),tol);
[tf3,idx3] = ismembertol(u(half_1st),Est3(half_1st),tol);
% Define the rearranged vectors
Est1_new = Est1([half_1st(idx1) half_2nd(idx1)]);
Est2_new = Est2([half_1st(idx2) half_2nd(idx2)]);
Est3_new = Est3([half_1st(idx3) half_2nd(idx3)]);

11 Comments

Thank you very much for responding. But I think I could not explain my question properly. I re-explain it.
My reference vector is u=[0.5 1 1.5 0.6981 1.3962 1.5707];
Now I want to estimate this vector with the help of Flower Pollination Algorithm. When I run this algorithm, it gives me a vector of the same size as u but the values inside estimated vector are not exactly the same as in u but are nearly identical to the values in u. Then I check it whether the elements in the estimated vector have swapped their position or not? If change has occured, then I re-aarange them manually and save it in an excel file.Then I run the algorithm again to get next vector which will be my estimated vector.This time again I check whether elements have changed their position or not and so on.
I want to keep a check on the estimated vector in such a way that irrespective of the position change by elements in the estimated vector, Matlab should re-arrange it on the same pattern as is the pattern of my vector u.Then I will just copy it and save it in Excel rather than re-arranging manually. Regards
OK.
Did you try to understand what my algorithm is doing? Given the input vectors u and Est2, for example
u=[0.5 1 1.5 0.6981 1.3962 1.5707];
Est2=[0.9999 0.5001 1.49999 1.3990 0.6999 1.5880];
it recognizes that Est2 is like u, except that it is rearranged. So it outputs the rearranged vector
Est2_new=[0.5001 0.9999 1.5000 0.6999 1.3990 1.5880]
Isn't this exactly what you just asked for?
I run my algorithm nn times via a for loop and I keep estimated vector in variable temp. For this I have written my code as :
if abs((temp(nn,1)-u(2)))< abs((temp(nn,1)-u(1)))
two(nn,:)=[temp(2) temp(1) temp(3) temp(5) temp(4) temp(6)];
elseif abs((temp(nn,1)-u(3)))< abs((temp(nn,1)-u(1)))
two(nn,:)=[temp(3) temp(2) temp(1) temp(6) temp(5) temp(4)];
elseif abs((temp(nn,2)-u(3)))< abs((temp(nn,2)-u(2)))
two(nn,:)=[temp(1) temp(3) temp(2) temp(4) temp(6) temp(5)];
else
two(nn,:)=temp(nn,:);
end
But this does not work and the positions get changed inspite of this check. So what logic can I have for this? Regards
@Sadiq Akbar: rather than writing this complex code
= [temp(2) temp(1) temp(3) temp(5) temp(4) temp(6)]
simply use indexing lik this:
idx = [2,1,3,5,4,6];
= temp(idx);
and then the solution to your original question will be much simpler (hint: use idx).
@Sadiq,
I think your algorithm does not work because
abs((temp(nn,1)-u(2)))< abs((temp(nn,1)-u(1)))
can be true, even if you actually want to swap element 1 and 3, not 1 and 2.
My algorithm, using your variable names, would be
% You only need to do this part once
half_1st = 1:3;
half_2nd = 4:6;
tol = 0.01; % Set this to a value that is "close enough" to the original value
% This part would be in your FOR loop
[tf,idx] = ismembertol(u(half_1st),temp(nn,half_1st),tol);
two(nn,:) = temp(nn,[half_1st(idx) half_2nd(idx)]);
Thank you very much for your help. But when I implemented your code, Matlab gives me this error:
Array indices must be positive integers or logical values.
Error in servercode_14_11_2019 (line 30)
two(nn,:) = temp(nn,[half_1st(idx) half_2nd(idx)]);
% Inputs
u=[0.5 1 1.5 0.6981 1.3962 1.5707];
temp = [0.499 1.002 1.5001 0.6890 1.3880 1.49998
0.9999 0.5001 1.49999 1.3990 0.6999 1.5880
0.5001 1.4981 1.0012 0.7000 1.57160 1.39990
1.49999 0.9999 0.5001 1.5880 1.3990 0.6999];
% Preallocate the output array
out = zeros(size(temp));
% Parameters
half_1st = 1:3;
half_2nd = 4:6;
tol = 0.01; % Set this to a value that is "close enough" to the original value
% Rearrange and save the output
for nn = 1:size(temp,1)
[tf,idx] = ismembertol(u(half_1st),temp(nn,half_1st),tol);
out(nn,:) = temp(nn,[half_1st(idx) half_2nd(idx)]);
end
Thank you veru much for your help. But aghain I got the same error as above. Let me re-explain my problem.
My reference vector
u=[0.5 1 1.5 0.6981 1.3962 1.5707];
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
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
Positions 1 and 3 interchange
Positions 2 and 3 interchange
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
I beileve I understand your problem.
In an earlier comment, I already explained why your algorithm doesn't quite work. Let me try again, with a simple example where it fails. Look at this:
% Inputs
u=[0.5 1 1.5 0.6981 1.3962 1.5707];
temp = [1.49999 0.9999 0.5001 1.5880 1.3990 0.6999];
nn = 1;
abs((temp(nn,1)-u(2))) < abs((temp(nn,1)-u(1))) % This is the first line of your algorithm.
Your algorithm is testing if element 2 of u is closer than element 1. It is! So, you swap elements 1 and 2, and you are done.
But that is wrong in this case. Element 3 is even closer. You actually want to swap elements 1 and 3.
So, I think you should try to fix your testing cases so that you cover this possibility.
Regarding my solution, you keep saying it does not work, and reporting the error that MATLAB is reporting. But have you tried to understand why you are getting that error, or how my solution should work in the first place?
I suggest you try adjusting the tolerance value, which might be too small. Maybe try setting
tol = 0.1; % Instead of tol = 0.01
and see if that solves it.

Sign in to comment.

half_1st = 1:3;
half_2nd = 4:6;
tol = 0.1;
u=[0.5 1 1.5 0.6981 1.3962 1.5707];
nn=0;
for n=100:199
nn=nn+1;
[out1,out2,out3]=flower(n,Remaining input arguments---)
temp(nn,:)=out1;
------------
-----------
%-------------Check for swapping-----------------
[tf,idx] = ismembertol(u(half_1st),temp(nn,half_1st),tol);
two(nn,:) = temp(nn,[half_1st(idx) half_2nd(idx)]);
end
When I run the above code, I get the following error even though I have reduced value of tol to 0.1 as per your instruction:
Subscript indices must either be real positive integers or logicals.
Error in servertestingMathworks (line 26)
two(nn,:) = temp(nn,[half_1st(idx) half_2nd(idx)]);

3 Comments

Can you post the value of out1 for which this algorithm does not work?
I ran it several times,and each time I get different values of out1 given below:
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
and so on and each time it gives me the same error
OK, so it is clear (to me) why my algorithm isn't working. Do you see how your original example input
Est1=[0.499 1.002 1.5001 0.6890 1.3880 1.49998]
Est2=[0.9999 0.5001 1.49999 1.3990 0.6999 1.5880]
Est3=[0.5001 1.4981 1.0012 0.70001 1.57160 1.39990]
the values are all very close to [0.5 1.0 1.5]? The biggest discrepancy is only about 0.002! I assumed that that was always true, but your new inputs
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
can be much further away from [0.5 1.0 1.5].
Neither my algorithm nor your algorithm, which both rely on proximity to the original vector, will work for this input. I have another idea that I will post later.

Sign in to comment.

Categories

Find more on Particle & Nuclear Physics 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!