Search code examples
arraysmatlabrandomcell

Matlab, Change order in a cell array randomly


is it possibly to change the order of a cell array randomly with a function, or shoudl I another way around ?


Solution

  • Use randperm:

    >> myCell = {'a', 23, [3 4 5], 'bbb'}
    myCell = 
        'a'    [23]    [1x3 double]    'bbb'
    
    >> myCell(:) = myCell(randperm(numel(myCell)))
    myCell = 
        'bbb'    'a'    [1x3 double]    [23]
    

    This works for n-dimensional cell arrays too:

    >> myCell = {1, 2; 'a', 'b'}
    myCell = 
        [1]    [2]
        'a'    'b'
    
    >> myCell(:) = myCell(randperm(numel(myCell)))
    myCell = 
        [1]    'a'
        'b'    [2]