6 views (last 30 days)

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?

Thiago Henrique Gomes Lobato
on 16 Feb 2020

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

Thiago Henrique Gomes Lobato
on 16 Feb 2020

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

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.