Search code examples
matlabmodecell-array

Matlab: Find the most repeated value in a cell (revised)


To my code idxcell{:,1} for 1:10 iterations gives the following results:

ans =
         9    10    14    15    19    20    24    25
    ans =
      Columns 1 through 13
         7     8    11    12    13    14    16    17    18    19    21    22    23
      Column 14
        24
    ans =
        13    14    15    18    19    20    23    24    25
    ans =
         6     7    11    12    16    17    21    22
    ans =
        16    17    21    22
    ans =
         6     7    11    12    13    16    17    21
    ans =
         4     5     8     9    10    13    14    15    19    20
    ans =
         4     5     8     9    10    14    15
    ans =
        11    12    13    14    16    17    18    19    21    22    23    24
    ans =
         1     2     3     6     7     8    11    12    13

How can i get the most repeated value of those cell's elements (i think on that paradigm is the '14')? I dont want index of it only the value. I have tried

idxcell{:,1}
temp = idxcell{:,1};
M = mode(temp)

but got result for first cell only and i am not sure that mode() is what i have to use for my purpose.


This was answered by angainor: mode([idxcell{:}])


Revised: Also is it possible to search for all cell arrays/rows in once for i iterations to find the most repeated value and sort the results, most repeated to less, inside a cell array or matrix in descending order?


|Answered by me after angainor's tip: Sort = sort([idxcell{:}])


Thanks in advance.


Solution

  • If I understand correctly, you need to concatenate all the cell vectors into one before you call mode

    mode([idxcell{:}])
    ans =
    
    13
    

    If you want to get the most frequently appearing value for all cells, you can use cellfun

    cellfun(@mode, idxcell)
    ans =
    
     9    10    14    15    19    20    24    25