How to re-arrange swapped elements in an estimated vector as compared to a reference vector
2 views (last 30 days)
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
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.
Accepted Answer
the cyclist
on 16 Nov 2019
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.
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
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 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.
See Also
Categories
Find more on Logical 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!