Azzera filtri
Azzera filtri

finding unique cell elements from different size of matrix

4 visualizzazioni (ultimi 30 giorni)
Hello friends, I have a cell array, but each cell has a different length. For example, a={[1,2] [1,4,2,5] [1,3,4,5,2] [0,5,6] [7,2,4,5] [1,4,2,5,6,8,9]}. Two questions. I want to find elements that are most often repeated in each cell. In this example, the answer is '2'. Note that '2' is not present in cell number 5. The second question, I want to know is how many times answer '2' is repeated. Previous examples deal with uniform cell sizes using the unique function. In this example cell sizes are not constant. Any help, thanks in advance. -DaveG

Risposte (1)

Stephen23
Stephen23 il 16 Ott 2018
Modificato: Stephen23 il 16 Ott 2018
Your question has nothing to do with unique. The element that occurs the most often in a set of data is called the mode, and it can be obtained using the MATLAB function mode:
>> a = {[1,2],[1,4,2,5],[1,3,4,5,2],[0,5,6],[7,2,4,5],[1,4,2,5,6,8,9]};
>> v = [a{:}]; % convert to numeric vector
>> m = mode(v) % most common element
m = 2
>> n = nnz(m==v) % how many times it occurs
n = 5
  1 Commento
Research
Research il 16 Ott 2018
Thanks a lot. This worked. I could have ever imagined there is a function exist called 'nnz'.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by