Calculate min/max/mean value every 100 points
32 views (last 30 days)
Show older comments
Hey! I have an array with 17 999 elements and for every 100 points I want to calculate min, max and mean values. First I thought about using reshape and convert the array into a table with 100 rows and just calculate what I need for every column but it doesn't work with the number of elelmts that I have. I am wondering if there is any other way?
1 Comment
Image Analyst
on 15 Feb 2022
Does your window of 100 elements slide along one element at a time (see movmean, etc), or does it move in "jumps" of 100 elements? What are your plans for the last 99 points? Why is your vector not a multiple of 100?
Answers (3)
Rik
on 15 Feb 2022
Reshape will work, but you have to pad with NaN values and set the 'omitnan' flag:
data=rand(1,17999);
data(end+(1:mod(-end,100)))=NaN;
data=reshape(data,100,[]);
mean(data,'omitnan')
0 Comments
David Hill
on 15 Feb 2022
Just remove the last 99 elements. If you need the mean, max, min for the last 99 elements, calculate them separately.
r=reshape(yourMatrix(1:17900),100,[]);
Min=min(r);
Max=max(r);
Mean=mean(r);
0 Comments
William Rose
on 15 Feb 2022
N=17999;
M=1000;
P=floor(N/M);
x=rand(1,N);
xavg=zeros(1,P); %allocate array for average values
for i=1:P
xavg(i)=mean(x((i-1)*M+1:i*M));
end
plot(1:P,xavg,'-r*')
Try it. Good luck.
1 Comment
William Rose
on 15 Feb 2022
@Liliya Vasilevich, Now I see that you requested every 100 points, and I wrote a script for 1000 points. Change M from 1000 to 100 to get the average every 100 points.
See Also
Categories
Find more on Logical 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!