# I need to make groups from array

5 views (last 30 days)

Show older comments

### Accepted Answer

Walter Roberson
on 30 Jun 2021

A = [.06, .18, .15, .14, .13, .12, .125, .15, .105, .115, .34, .32, .03 .14]

mask = A(2:end) > 1.05 * A(1:end-1);

group_lengths = diff([0, find(mask), length(A)])

mat2cell(A, 1, group_lengths)

##### 6 Comments

### More Answers (1)

David Hill
on 30 Jun 2021

Some guessing. You only said it stays in group is greater by 1-5%, I assumed a new group otherwise. Anyways, you modify the below to meet your needs.

n{1}=yourArray(1);

c=1;

for k=2:length(yourArray)

if yourArray(k)<=1.05*yourArray(k-1)&&yourArray(k)>=1.01*yourArray(k-1)

n{c}=[n{c},yourArray(k)];

else

c=c+1;

n{c}=yourArray(k);

end

end

##### 2 Comments

Walter Roberson
on 30 Jun 2021

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!