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

2 次查看(过去 30 天)
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 个评论
the cyclist
the cyclist 2019-11-13
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
Sadiq Akbar 2019-11-13
编辑:the cyclist 2019-11-13
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,

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2019-11-16
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.

更多回答(2 个)

the cyclist
the cyclist 2019-11-13
编辑:the cyclist 2019-11-13
% 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 个评论
the cyclist
the cyclist 2019-11-15
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
the cyclist 2019-11-15
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
Sadiq Akbar 2019-11-15
编辑:the cyclist 2019-11-15
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 个评论
Sadiq Akbar
Sadiq Akbar 2019-11-15
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
the cyclist
the cyclist 2019-11-16
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by