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

15 views (last 30 days)
Szabó-Takács Beáta on 16 Jan 2019
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
unique(x)
fuction. Can someone suggest me a solution?

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)]
Szabó-Takács Beáta on 21 Jan 2019
Thank you very much! It works.