How to order variables by their size?

6 views (last 30 days)
Hi all,
Assuming I have these time-points, marking the start of a condition... I have 3 conditions (names = A, B and C), each with 5 consecutive blocks starting at (onsets)...
So onset times of these conditions are:
onsets{1,1} = [7848 8477 9099 9699 10315]
onsets{1,2} = [10923 11548 12136 12748 13353]
onsets{1,3} = [4565 5359 5985 6580 7211]
These correspond to:
names{1,1} = "A"
names{1,2} = "B"
names{1,3} = "C"
i.e. A_onsets = [7848 8477 9099 9699 10315]
As can be seen, when I sum these time-points (of onsets), the order of presentation was actually C, A, B.
sum_A = 45438 %sum_A = sum(onsets{1,1}))
sum_B = 60708 %sum_B = sum(onsets{1,2})
sum_C = 29700 %sum_C = sum(onsets{1,3})
How can I return an array of the order of the presentation of the conditions... e.g. order = ["C", "A", "B"]
Many many thanks in advance :)
R

Accepted Answer

Star Strider
Star Strider on 22 Mar 2019
Try this:
[~,ix] = sort(cellfun(@sum, onsets));
Out = names(ix)
producing:
Out =
1×3 cell array
{["C"]} {["A"]} {["B"]}
  2 Comments
Richie
Richie on 22 Mar 2019
This is beautiiful!
Thank you so much.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!