How to re-arrange swapped elements in an estimated vector as compared to a reference vector
Show older comments
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
Johannes Fischer
on 13 Nov 2019
How do you determine you that two values have swapped position?
the cyclist
on 13 Nov 2019
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.
Sadiq Akbar
on 13 Nov 2019
Edited: the cyclist
on 13 Nov 2019
Accepted Answer
More Answers (2)
the cyclist
on 13 Nov 2019
Edited: the cyclist
on 13 Nov 2019
% 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
Sadiq Akbar
on 13 Nov 2019
the cyclist
on 13 Nov 2019
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?
Sadiq Akbar
on 13 Nov 2019
Edited: the cyclist
on 13 Nov 2019
= [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).
the cyclist
on 13 Nov 2019
Edited: the cyclist
on 14 Nov 2019
@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.
the cyclist
on 13 Nov 2019
Edited: the cyclist
on 13 Nov 2019
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)]);
Sadiq Akbar
on 14 Nov 2019
the cyclist
on 14 Nov 2019
% 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
Sadiq Akbar
on 15 Nov 2019
Edited: the cyclist
on 15 Nov 2019
the cyclist
on 15 Nov 2019
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.
the cyclist
on 15 Nov 2019
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.
Sadiq Akbar
on 15 Nov 2019
Edited: the cyclist
on 15 Nov 2019
3 Comments
the cyclist
on 15 Nov 2019
Can you post the value of out1 for which this algorithm does not work?
Sadiq Akbar
on 15 Nov 2019
the cyclist
on 16 Nov 2019
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.
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!