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

9 visualizaciones (últimos 30 días)
Szabó-Takács Beáta
Szabó-Takács Beáta el 16 de En. de 2019
Comentada: Szabó-Takács Beáta el 21 de En. de 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?

Respuestas (1)

Guillaume
Guillaume el 16 de En. de 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)]

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by