Matlab - count how many times a number occurs

  • Context: MATLAB 
  • Thread starter Thread starter jemma
  • Start date Start date
  • Tags Tags
    Count Matlab
Click For Summary
SUMMARY

The discussion focuses on counting the occurrences of specific sets of numbers in a matrix using MATLAB. The provided matrix 'x' contains two distinct sets: '1 2 3 4' and '5 6 7 8'. The suggested approach to count occurrences is to utilize the MATLAB function 'sum' in conjunction with logical indexing, specifically 'sum(x == 2)' to count the occurrences of the number 2. However, for counting entire rows, a more comprehensive function is required to tally the frequency of each unique row.

PREREQUISITES
  • Familiarity with MATLAB syntax and functions
  • Understanding of matrix operations in MATLAB
  • Knowledge of logical indexing in MATLAB
  • Basic concepts of counting and frequency analysis
NEXT STEPS
  • Explore MATLAB's 'unique' function for identifying distinct rows in a matrix
  • Learn how to implement a custom function to count occurrences of entire rows
  • Research MATLAB's 'histcounts' function for frequency distribution
  • Investigate the use of 'cell arrays' for handling non-numeric data in MATLAB
USEFUL FOR

Data analysts, MATLAB users, and anyone interested in performing frequency analysis on numerical matrices in MATLAB.

jemma
Messages
35
Reaction score
0
Suppose

x =

1 2 3 4
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8

I need a function to count how many times each set of number occurs
eg.
1 2 3 4 occurs 3 times
5 6 7 8 occurs 2 times

Thanks!
 
Last edited:
Physics news on Phys.org
Off the top of my head, you could try:

sum(x == 2)

To count the number of times 2 appears.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K