2

I have this vector something like this

1
2
2
2
3
3
3
3
.
.
.

What I want to know is in how many rows the individual numbers appeared something like this

1 1
2 3
3 4
. .
. .

I can loop through each element and use

index = find(vector == element)
length(index)

But that is very inefficient. What is the most efficient way to do it in matlab?

user34790
  • 2,020
  • 7
  • 30
  • 37

1 Answers1

1

From the documentation of histc:

bincounts = histc(x,binranges) counts the number of values in x that are within each specified bin range.

If you combine histc with unique, you can get what you want:

a =
     4
     2
     3
     3
     1
     2
     1
     1
     2
     3

uni = unique(a);
[uni, histc(a,uni)]
ans =
     1     3
     2     3
     3     3
     4     1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70