File Exchange

image thumbnail

COUNT_MATRIX_ELEMS

version 1.0 (2.56 KB) by

Obtains unique row elements from MxN matrix A and counts the number of occurrences.

1 Download

Updated

View License

COUNT_MATRIX_ELEMS - Obtains unique row elements from MxN matrix A and counts the number of occurrences. UN contains all matrix A distinct elements. C will have the same size as UN. UN(k) has C(k) occurrences in matrix A.

   Example:
 
       A = [1 2 3; 4 5 6; 1 2 3; 4 5 7; 1 2 1; 1 2 3; 4 5 6]
       [UN, C] = count_matrix_elems(A)

 A =
 
      1 2 3
      4 5 6
      1 2 3
      4 5 7
      1 2 1
      1 2 3
      4 5 6
      
 UN =
      1 2 1
      1 2 3
      4 5 6
      4 5 7
  
 C =
      1
      3
      2
      1
 
   See also UNIQUE, FIND, NUMEL

Comments and Ratings (1)

Jan Simon

Jan Simon (view profile)

I'd prefer HISTC to count the occurences. But your approach "numel(find(j == a))" can be accelerated as "sum(j == a)".

The documentation of your code is excellent: H1 line, help text, input, output, example, date and name of the author, comment lines.

MATLAB Release
MATLAB 7.14 (R2012a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

count_matrix_elems/