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

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

### Categories

Find more on Call C++ from MATLAB 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!

Translated by