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

조회 수: 12 (최근 30일)
Szabó-Takács Beáta
Szabó-Takács Beáta 2019년 1월 16일
댓글: Szabó-Takács Beáta 2019년 1월 21일
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?

답변 (1개)

Guillaume
Guillaume 2019년 1월 16일
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)]

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by