How to counting frequency of a value occurence in a matrix?

15 views (last 30 days)
Dear All,
I have a vector which values are 1:31. I would like to calculate the frequency of occurence these values in different matrixes. I tried the following way:
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
out = [a, histc(x(:),a)];
but I got the following error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Some value of a does not exist in the matrix thust I cannot use
fuction. Can someone suggest me a solution?

Answers (1)

Guillaume on 16 Jan 2019
Since you're passing a column vector to histc (X(:)), the output is going to be a column vector. Hence you're trying to concatenate a row vector (a) with a column vector. Indeed the dimension of matrices being concatenated are never going to be consistent. The easiest is to transpose a.
Note that the way you create a is overly complicated. You must like typing!
a = 1:31;
out = [a', histc(X(:), a)]


Community Treasure Hunt

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

Start Hunting!