Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

fetching data from array

Asked by kash

kash (view profile)

on 19 Sep 2013

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

0 Comments

kash

kash (view profile)

Products

No products are associated with this question.

2 Answers

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 19 Sep 2013
Edited by Andrei Bobrov

Andrei Bobrov (view profile)

on 19 Sep 2013
Accepted answer
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)]

0 Comments

Andrei Bobrov

Andrei Bobrov (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 19 Sep 2013
Edited by Jan Simon

Jan Simon (view profile)

on 19 Sep 2013

With: FEX: RunLength

[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 Comment

kash

kash (view profile)

on 20 Sep 2013

Thanks Jan for ur answer

Jan Simon

Jan Simon (view profile)

Contact us