Not able to get count of each elements in array itself

조회 수: 1 (최근 30일)
Scott Tehelson
Scott Tehelson 2020년 6월 30일
댓글: Scott Tehelson 2020년 6월 30일
I am having this array..x=[ 1 2 3 1 2]
Now I want to replace it with count of the corresponding elements..here x should get modified to x=[2 2 1 2 2]
By using hist(x, unique(x)) I am able to do it but it gives the count for unique elements. I want the result to have the number of elements.
please help!

채택된 답변

Gaganjyoti Baishya
Gaganjyoti Baishya 2020년 6월 30일
Hi Scott,
Instead of using the histc function, you can use the grouptransform function to get the count of all elements int the array.
x = [1 2 3 1 2];
x = [1 2 3 1 2]';
x = grouptransform(x,x,@numel);
x contains the required array.

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 6월 30일
[~, ~, c] = unique(x);
v = accumarray(c, 1);
Wanted = v(c)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by