Simo Os
Simo Os

Reputation: 153

Sort rows of a cell array using given indexes

We are given a cell array Ref_M of size m x n, and a vector IND of length m.

IND(i) contains the number of non-empty cells in the ith row of Ref_M. The purpose is to organize the lines in the cell array Ref_M based on the value in the IND vector from the largest value to the smallest.

Given:

Ref_M = [2x2 double]    [2x2 double]    []
        [2x2 double]     []             [] 
        [2x2 double]    [2x2 double]    []
        [2x2 double]    [2x2 double]    [2x2 double]

IND = [ 2 1 2 3]

The result should be:

New_Ref_M = [2x2 double]    [2x2 double]    [2x2 double]
            [2x2 double]    [2x2 double]    []
            [2x2 double]    [2x2 double]    []
            [2x2 double]    []              [] 

Also, is there a method to organize the cell array Ref_M lines without using the given vector of indexes, IND?

Upvotes: 0

Views: 200

Answers (1)

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27201

Try sort().

[B, transform] = sort(IND, 'descend');
New_Ref_M = Ref_M(transform, :);

You can determine IND on your own using:

IND = sum(~cellfun('isempty', Ref_M), 2);

Upvotes: 1

Related Questions