필터 지우기
필터 지우기

convert an array into its counting sequence..

조회 수: 1 (최근 30일)
VISHWATEJ N
VISHWATEJ N 2019년 8월 2일
편집: madhan ravi 2019년 8월 2일
i want to convert an array into its counting sequence. for example
if the input is :
x = [1 1 4 4 6 7]
two 1's two 4's one 6 one 7...
output must be :
y=[2 1 2 4 1 6 1 7]
can anyone help?

답변 (2개)

madhan ravi
madhan ravi 2019년 8월 2일
편집: madhan ravi 2019년 8월 2일
According to what I understand you want to count the number of occurances of each unique elements in the array x, then:
x = [1 1 4 4 6 7];
[u,~,idx]=unique(x);
v=accumarray(idx,1);
y=reshape([v,u(:)].',1,[])
Gives:
y =
2 1 2 4 1 6 1 7
  댓글 수: 2
Jos (10584)
Jos (10584) 2019년 8월 2일
This fails for inputs like [1 1 2 1 1], where values are repeated.
I admit, the question is a little unclear, but "counting sequence" suggests, at least to me, more than just counting ;-)
madhan ravi
madhan ravi 2019년 8월 2일
I'm not sure either let the OP verify it ;)

댓글을 달려면 로그인하십시오.


Jos (10584)
Jos (10584) 2019년 8월 2일
This is called run-length encoding, for which there are very efficient functions available on the File Exchange.
I point you to my own function RUNINDEX which can also return this:
[~, RLE] = runindex(x)
y = reshape(RLE(:,[3 1]).', 1, []) % your format

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by