필터 지우기
필터 지우기

How to sort a dot indexed array by rows based on one column

조회 수: 1 (최근 30일)
chaker ben salem
chaker ben salem 2022년 5월 21일
댓글: Seth Furman 2022년 5월 30일
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

채택된 답변

Jan
Jan 2022년 5월 21일
[~, index] = sort([pop.F]);
pop2 = pop(index);
  댓글 수: 2
chaker ben salem
chaker ben salem 2022년 5월 21일
i was thinking it could be done within the same table but that works perfectly.
thank you,
Seth Furman
Seth Furman 2022년 5월 30일
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)
t = 2×2 table
a b ________________ _ 1 2 3 4 2 5 6 7 8 1
sortrows(t,"b")
ans = 2×2 table
a b ________________ _ 5 6 7 8 1 1 2 3 4 2

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by