fetching data from array

조회 수: 4 (최근 30일)
kash
kash 2013년 9월 19일
I have a set of values as
G=[1 1 1 1 2 2 1 2 3 2 2 1 3 3]
in this there are 14 values,i want to fetch as following
there are 4 ones continously so i want to take first 1 ,then 2 2's continously ,i need first 2 ,then only 1 ,so i need to take that
So the output will be [1 2 1 2 3 2 1 3] with index positions [1 5 7 8 9 10 12 13]
kindly help

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 9월 19일
편집: Andrei Bobrov 2013년 9월 19일
G=[1 1 1 1 2 2 1 2 3 2 2 1 3 3];
G1 = G(:);
l = [true;diff(G1(:))~=0];
out = [G1(l),find(l)]

추가 답변 (1개)

Jan
Jan 2013년 9월 19일
편집: Jan 2013년 9월 19일
[value, number, index] = RunLength(G)
Then value and index are the wanted results.
If you have problems to compile the fast MEX file, you can use RunLength_M from this submission instead. If you process millions of elements and runtime matters, be sure to use the compiled version.
  댓글 수: 1
kash
kash 2013년 9월 20일
Thanks Jan for ur answer

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

카테고리

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