I need to make groups from array
조회 수: 14 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2021년 6월 30일
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
Walter Roberson
2021년 7월 1일
A = [.06, .18, .15, .14, .13, .12, .125, .15, .105, .115, .34, .32, .03 .14]
mask = A(2:end) < 0.95 * A(1:end-1) | A(2:end) > 1.05 * A(1:end-1);
group_lengths = diff([0, find(mask), length(A)])
mat2cell(A, 1, group_lengths)
추가 답변 (1개)
David Hill
2021년 6월 30일
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
Walter Roberson
2021년 6월 30일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!