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 Kommentare

Walter Roberson
Walter Roberson am 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
zhoug zho am 30 Jun. 2021
Like first element of this arrays is 0.06 that is lower than next element (0.1800),
So if 0.1800 is higher than [ 5% of 0.06+0.06], then new group will start from 0.1800. and previous group will attain only one element 0.06 (1st group).
In this way, third element is 0.15, now if this element 0.15 is higher than [ 5% of 0.1800+0.1800](previous elemnt+5% of previous element), then new group will start from 0.1300 (3rd group). otherwise if it is lower, then 0.15 will be the part of previous group (2nd group with element 0.1800) . and so on.
Walter Roberson
Walter Roberson am 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
zhoug zho am 30 Jun. 2021
Yes I need to compare all adjacent elements to form groups. Like firstly I compare element one and two and then two and three and then three and four, and so on.
zhoug zho
zhoug zho am 30 Jun. 2021
can you give an example based on our discussion, . then i will try to adjust the loop according to my need.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 30 Jun. 2021

1 Stimme

A = [.06, .18, .15, .14, .13, .12, .125, .15, .105, .115, .34, .32, .03 .14]
A = 1×14
0.0600 0.1800 0.1500 0.1400 0.1300 0.1200 0.1250 0.1500 0.1050 0.1150 0.3400 0.3200 0.0300 0.1400
mask = A(2:end) > 1.05 * A(1:end-1);
group_lengths = diff([0, find(mask), length(A)])
group_lengths = 1×6
1 6 2 1 3 1
mat2cell(A, 1, group_lengths)
ans = 1×6 cell array
{[0.0600]} {[0.1800 0.1500 0.1400 0.1300 0.1200 0.1250]} {[0.1500 0.1050]} {[0.1150]} {[0.3400 0.3200 0.0300]} {[0.1400]}

6 Kommentare

zhoug zho
zhoug zho am 30 Jun. 2021
thanks. got it. one thing is confusing me, why you used 1.05 ?
Walter Roberson
Walter Roberson am 30 Jun. 2021
x + 5% of x is 100% of x plus 5% of x, collect the x to get (100+5) = 105% of x. Expressed as a fraction, that is 1.05 * x
zhoug zho
zhoug zho am 30 Jun. 2021
Thanks for the help. I appreciate it.
zhoug zho
zhoug zho am 1 Jul. 2021
Hi walter, if i add another condition x + - 5% of x (mean now x plus/minus 5%), so in this way i will set a threhold a like 95% to 105%, how can i set this in the existing code line.
A = [.06, .18, .15, .14, .13, .12, .125, .15, .105, .115, .34, .32, .03 .14]
A = 1×14
0.0600 0.1800 0.1500 0.1400 0.1300 0.1200 0.1250 0.1500 0.1050 0.1150 0.3400 0.3200 0.0300 0.1400
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)])
group_lengths = 1×13
1 1 1 1 1 2 1 1 1 1 1 1 1
mat2cell(A, 1, group_lengths)
ans = 1×13 cell array
{[0.0600]} {[0.1800]} {[0.1500]} {[0.1400]} {[0.1300]} {[0.1200 0.1250]} {[0.1500]} {[0.1050]} {[0.1150]} {[0.3400]} {[0.3200]} {[0.0300]} {[0.1400]}
zhoug zho
zhoug zho am 1 Jul. 2021
Bearbeitet: zhoug zho am 1 Jul. 2021
got it... Thank you so much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

David Hill
David Hill am 30 Jun. 2021

1 Stimme

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 Kommentare

zhoug zho
zhoug zho am 30 Jun. 2021
Thanks David,
Walter Roberson
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by