# How to write a matlab program to arrange my workspace data?

6 views (last 30 days)
Sadiq Akbar on 16 Feb 2020
Commented: Sadiq Akbar on 17 Feb 2020
I have a desired vector u=[0.5 1 2 40 70 90]. I give this vector to a metaheuristic algorithm and I run the algorithm. The algorithm runs 100 times and the algorithm does the following for me:
1-Estimates 100 such vectors for me and sotres then in variable Position, but the elements inside the estimated vectors are not in the same order as in the given vector u.
2- Estimates 100 values of fitness and stores them in variable Cost.
3- Estimates 100 velocities of the same size as u and sotres them in variable Velocity
And all above variables are stored in struct BestSol. I want to write a matlab program such that all these estimated vectors in point1 bove are sotred in the same order as u. All values in point2 above are in descending order and all values in point3 above are also in same order as u. Can any body helep me in this regard?

You could use the function sort to sort your arrays and get the index of the sorting, so you can apply this indexing to u and give the index in point 3, for example. A little example to show the functionality:
Positions = [0.5 2 40 1 70 90]
[SortedPositions,Index] = sort(Positions);
SortedPositions
Index
SortedPositions =
0.5000 1.0000 2.0000 40.0000 70.0000 90.0000
Index =
1 4 2 3 5 6

Sadiq Akbar on 16 Feb 2020
.....................................
....................................
.....................................
BestCost(it)=GlobalBest.Cost;
end
BestSol (k)= GlobalBest;
Afields = fieldnames(BestSol);
Acell = struct2cell(BestSol);
sz = size(Acell);
Acell = reshape(Acell, sz(1), []);
Acell = Acell';
Acell = sortrows(Acell, 2);
Acell = reshape(Acell', sz);
BestSol = cell2struct(Acell, Afields, 1);
end
%------------------End of Algorithm---------------------
Above are the last lines sof the algorithm. When I run the above algorithm, there is much stuff in the workspace, but the interest to me is the BestSol. When I double click on it, it is opened and I see there are five fields in this BestSol as I mentioned in my question. Now there are 100 estimated vectors sotred in Position, 100 scalar values stored in Cost and 100 Vecots sotred in Velocity. The size of Position and Velocity is the same as u. But as I told you, I want to re-arrange the elements of the 100 estimated vectors sotred in Position in the same order as u.
Could you save the BestSol structure and add here so I can download it and see it? This would make it way easier for me to help you. Still, considering that the BestSol has fields named Position and Velocity as you wrote it:
[BestSol.Position,pos] = sort(BestSol.Position,2);
BestSol.Velocity = BestSol.Velocity(pos);
This will sort the arrays in Position so they will be as close as possible as u (which, in your question, is sorted) and then save the indices so the velocity can be rearangend to the sorted positions. Is this what you want? If not maybe a small example of your problem and wished solution with a small matrix could be of help so I can better understand the problem
Sadiq Akbar on 17 Feb 2020
I have attached the BestSol structure. You can easily see it here. I repeat my problem. If you double click on the BestSol, you will see four fields in it. they will be Position, Cost, velocity and Voilation. I want to re-arrange the elements of each of the 100 estimated stored vectors in Position in such a way that they have the same order as my u vector has. i.e. let my u=[0.5 1 2 40 70 90]. Now the algorithm estimates 100 such vectors. But the elements of some of the estimated vectors are not in the same order as u. Say for example the algorithm estimates like these:
[0.4991 0.9981 2.001 39.9876 71.2 89.9999]
[ 1.001 0.5001 1.9999 70.0101 40.001 89.9999]-----Elements are not in same order as that of u
[0.4991 2.0201 0.9981 40.0001 90.0001 71.2]------Again order is not same as that of u.
[ 2.0102 0.9981 0.4991 89.9999 71.2 39.95534 ]-----Again order is not same as that of u
In short the algorithm estimates 100 such vectors in which the position of some elements get changed. I want to re-arrange them in the same order as that of u.