Be the first to rate this file! 6 Downloads (last 30 days) File Size: 2.56 KB File ID: #38680

COUNT_MATRIX_ELEMS

by

 

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

| Watch this File

File Information
Description

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

MATLAB release MATLAB 7.14 (R2012a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (1)
22 Oct 2012 Jan Simon

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.

Contact us