Condensing code cannnot figure out easier function to use

1 vue (au cours des 30 derniers jours)
Keaton Looper
Keaton Looper le 25 Mar 2022
Modifié(e) : Matt J le 26 Mar 2022
I know that the actual data is not included on this but i am trying to find a way to read groups of 300 data points at a time, sum up the amount of ones in each group, and compare it to a condtion to count as either a 1 or 0. I included down below the way that i am originally doing it but it is not very efficent. Anyone know what function i can use to shorten it?
max = 30
Z1 = difference(1:300); % assigning variables to individual increments of the original vector.
Z2 = difference(301:600);
Z3 = difference(601:900);
Z4 = difference(901:1200);
Z5 = difference(1201:1500);
Z6 = difference(1501:1800);
Z7 = difference(1801:2100);
Z8 = difference(2101:2400);
Z9 = difference(2401:2700);
Z10 = difference(2710:3000);
Z11 = difference(3001:3300);
Z12 = difference(3301:3600);
Z13 = difference(3601:3900);
Z14 = difference(3901:4200);
Z15 = difference(4201:4500);
Z16 = difference(4501:4800);
Z17 = difference(4801:5100);
Z18 = difference(5101:5400);
Z19 = difference(5401:5700);
Z20 = difference(5701:6000);
Z21 = difference(6001:6300);
Z22 = difference(6301:6600);
Z23 = difference(6601:6900);
Z24 = difference(6901:7200);
Z25 = difference(7201:7500);
Z26 = difference(7501:7800);
Z27 = difference(7801:8100);
Z28 = difference(8101:8400);
Z29 = difference(8401:8700);
Z30 = difference(8701:9000);
Z31 = difference(9001:9268);
W1 =sum(Z1); % summing amount of ones in each vector
W2 =sum(Z2);
W3 =sum(Z3);
W4 =sum(Z4);
W5 =sum(Z5);
W6 =sum(Z6);
W7 =sum(Z7);
W8 =sum(Z8);
W9 =sum(Z9);
W10 =sum(Z10);
W11 =sum(Z11);
W12 =sum(Z12);
W13 =sum(Z13);
W14 =sum(Z14);
W15 =sum(Z15);
W16 =sum(Z16);
W17 =sum(Z17);
W18 =sum(Z18);
W19 =sum(Z19);
W20 =sum(Z20);
W21 =sum(Z21);
W22 =sum(Z22);
W23 =sum(Z23);
W24 =sum(Z24);
W25 =sum(Z25);
W26 =sum(Z26);
W27 =sum(Z27);
W28 =sum(Z28);
W29 =sum(Z29);
W30 =sum(Z30);
W31 =sum(Z31);
if W1 >= max % comparing amount of ones in each group to the max value or 30. If the number of ones exceed 30, then assign a 1 to the group, if not then assign a 0.
count = count + 1 % keeps track of how many times the number of ones in each group exceeds the max value of 30.
end
if W2 >= max
count = count + 1
end
if W3 >= max
count = count + 1
end
if W4 >= max
count = count + 1
end
if W5 >= max
count = count + 1
end
if W6 >= max
count = count + 1
end
if W7 >= max
count = count + 1
end
if W8 >= max
count = count + 1
end
if W9 >= max
count = count + 1
end
if W10 >= max
count = count + 1
end
if W11 >= max
count = count + 1
end
if W12 >= max
count = count + 1
end
if W13 >= max
count = count + 1
end
if W14 >= max
count = count + 1
end
if W15 >= max
count = count + 1
end
if W16 >= max
count = count + 1
end
if W17 >= max
count = count + 1
end
if W18 >= max
count = count + 1
end
if W19 >= max
count = count + 1
end
if W20 >= max
count = count + 1
end
if W21 >= max
count = count + 1
end
if W22 >= max
count = count + 1
end
if W23 >= max
count = count + 1
end
if W24 >= max
count = count + 1
end
if W25 >= max
count = count + 1
end
if W26 >= max
count = count + 1
end
if W27 >= max
count = count + 1
end
if W28 >= max
count = count + 1
end
if W29 >= max
count = count + 1
end
if W30 >= max
count = count + 1
end
if W31 >= max
count = count + 1
end
  1 commentaire
Catalytic
Catalytic le 25 Mar 2022
Modifié(e) : Catalytic le 25 Mar 2022
All 4 of your posted questions appear to be the same question phrased in somewhat different ways. You should not abandon a post just to clarify it in a different post. You should clarify yourself in your existing post.
Also, you have not accepted any of the previous answers, even though they seem valid for the question as you formulated it. You should not abandon a post without accepting an answer if/when a valid answer has been given.

Connectez-vous pour commenter.

Réponses (3)

Catalytic
Catalytic le 25 Mar 2022
Modifié(e) : Catalytic le 25 Mar 2022
threshold=30; %don't call this "max"
blocklen=300;
difference=rand(1,9268);
N=numel(difference);
Nc=ceil(N/blocklen)*blocklen;
difference(N+1:Nc)=nan;
W=sum( reshape(difference,blocklen,[]),1,'omitnan');
count=sum(W>=threshold)
count = 30

Torsten
Torsten le 25 Mar 2022
Modifié(e) : Torsten le 25 Mar 2022
number_of_elements = 9268;
blocklength = 300;
maximum = 30;
rest = mod(number_of_elements,blocklength)
number_of_elements_minus_rest = number_of_elements - rest;
number_of_blocks = number_of_elements_minus_rest/blocklength;
difference = 0.2*rand(1,number_of_elements);
difference = [reshape(difference(1:number_of_elements_minus_rest),number_of_blocks,blocklength); ...
[difference(number_of_elements_minus_rest+1:number_of_elements),zeros(1,blocklength-rest)]];
sum_of_blocks = sum(difference,2);
count = sum(sum_of_blocks >= maximum)

Matt J
Matt J le 26 Mar 2022
Modifié(e) : Matt J le 26 Mar 2022
thresh=30;
G=repelem(1:31,300)';
count = sum( accumarray(G(1:numel(differences)),differences(:)) > thresh);

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by