How to sort a dot indexed array by rows based on one column
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
chaker ben salem
am 21 Mai 2022
Kommentiert: Seth Furman
am 30 Mai 2022
i made a dot indexed array called "pop" using repmat like the following
pop.X = [];
pop.V = [];
pop.F = [];
pop.pBestX = [];
pop.pBestF = [];
pop = repmat(pop, N, 1)
and wanted to sort it by rows based on the columne "pop.F"
what i mean is when pop.F(3) change position to pop.F(5) all other pop.X/pop.V in the same row will follow it
the pop array i have looks like this one

if there's a better way to make this kind of table and sort it, it would be greatly apreciated
0 Kommentare
Akzeptierte Antwort
Jan
am 21 Mai 2022
[~, index] = sort([pop.F]);
pop2 = pop(index);
2 Kommentare
Seth Furman
am 30 Mai 2022
It's also worth mentioning that we can convert the struct array to a table using struct2table and sort the rows using sortrows.
s = [];
s(1).a = [1 2 3 4];
s(1).b = 2;
s(2).a = [5 6 7 8];
s(2).b = 1;
t = struct2table(s)
sortrows(t,"b")
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!