I need to make groups from array
Show older comments
I have this array, I need to make groups of these 14 elements with the rule that if value of a new element is 1 to 5 percent higher than the value of previous elements then the new element will be part of previous group, otherwise new group will start if it is higher than 5%. Thanks

5 Comments
Walter Roberson
on 30 Jun 2021
1 to 5 percent higher than what measurement? The running maximum of the group? If so then an implication would be that if the first value was 0.1 then the next value should form its own group if it is greater than 1.01*0.1 ?
What about the case where the new value is lower than the previous? Or if the value is greater by less than 1%?
zhoug zho
on 30 Jun 2021
Walter Roberson
on 30 Jun 2021
I suggest writing a loop.
Is the 5% always to be relative to the largest value in the current group? Or to the first value in the group? Or relative to the last value in the group (so in other words you would always be ending up comparing adjacent elements) ?
zhoug zho
on 30 Jun 2021
zhoug zho
on 30 Jun 2021
Accepted Answer
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
zhoug zho
on 30 Jun 2021
Walter Roberson
on 30 Jun 2021
However, if it is lower it needs to stay with the group being built according to https://www.mathworks.com/matlabcentral/answers/868868-i-need-to-make-groups-from-array#comment_1613113 and that means that you should not be testing against a lower bound. The 1% is a red herring; the only question is more than 5% increase (new group) otherwise continue the same group.
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!