joanna
joanna

Reputation: 85

sorting cell array in matlab and getting the new indices

I have H which is n by n cell array , each cell contains a vector of numbers that I want to sort in ascending order , this is the code that I have tried

   HH = cellfun(@sort,H, 'UniformOutput', false) 

the code worked perfectly but the problem that I want to have the indices of the element of vector,

for example: if a cell in this array contains [ 7 5 6 8] , it will be sorted as [ 5 6 7 8 ] & the indices are [2 3 1 4].

Upvotes: 3

Views: 90

Answers (1)

Dan
Dan

Reputation: 45741

According to Gnovice (Skipping outputs with anonymous function in MATLAB) you can just specify cellfun with two outputs!

[HH, HH_ind] = cellfun(@sort,H, 'UniformOutput', false) 

Upvotes: 3

Related Questions