function to find max values for intervals
Afficher commentaires plus anciens
I have a single column with many values . I need to find the maximum value for the first 152 numbers. I need matlab to also output the max for the next 151, then the next 151 as well as the next 151. Then I need the max for the next 152 and then the next 151 and this same pattern repeats for all the data. The alternating 152 is what makes this task seemingly impossible .
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 27 Déc 2019
It's not compact or cryptic but this brute force method is pretty easy to follow and understand.
r = rand(20418, 1); % Generate sample data.
counter = 1;
lenr = length(r)
for row = 1 : (152 + 3*151) : length(r)
row1 = row;
row2 = row1 + 151;
if row2 > lenr
break; % Quit if it would be beyond the end of the array.
end
fprintf('Getting means between row %d and %d, inclusive.\n', row1, row2);
theMeans(counter) = mean(r(row1:row2));
row1 = row2 + 1;
row2 = row1 + 150;
if row2 > lenr
break; % Quit if it would be beyond the end of the array.
end
fprintf('Getting means between row %d and %d, inclusive.\n', row1, row2);
theMeans(counter + 1) = mean(r(row1:row2));
row1 = row2 + 1;
row2 = row1 + 150;
if row2 > lenr
break; % Quit if it would be beyond the end of the array.
end
fprintf('Getting means between row %d and %d, inclusive.\n', row1, row2);
theMeans(counter + 2) = mean(r(row1:row2));
row1 = row2 + 1;
row2 = row1 + 150;
if row2 > lenr
break; % Quit if it would be beyond the end of the array.
end
fprintf('Getting means between row %d and %d, inclusive.\n', row1, row2);
theMeans(counter + 3) = mean(r(row1:row2));
counter = counter + 4;
end
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!