How to take the average every 5 data points?

I have a matrix 'GoogleDateClose' that is 251 by 2. The first column is just 1-252 to give a number assigned to each value in column 2. In column 2 I have all the data points. I need to write a script to take the mean of every 5 data points and put the resulting values into another matrix so I am able to plot those values. I feel like I did a poor job of describing what I am trying to get so please let me know if I can clarify.

댓글 수: 2

Do you mean take every 5th value (5, 10, 15) and then take the mean or take values 1-5 and calculate the mean and then take values 6-10 and calculate the mean
Noah Wilson
Noah Wilson 2018년 9월 18일
Take values 1-5 and take the mean then take values 6-10 and take the mean and so on and so forth.

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2018년 9월 18일

3 개 추천

251 or 251 is not a multiple of 5. So here it is for 250:
data = [(1:252)', rand(252, 1)] % Create sample data
column2 = data(:, 2);
out = reshape(column2(1:250), [], 5)
means5 = mean(out, 2)

댓글 수: 4

chris pamp
chris pamp 2022년 9월 10일
when i used it an error occured
Error using reshape
Product of known dimensions, 500, not divisible into total number of elements, 185790.
Is there any other way to perform the same thing for not divisible numbers?
numBlocks = 185790 / 250
numBlocks = 743.1600
numBlocks = 185790 / 251
numBlocks = 740.1992
numBlocks = 185790 / 252
numBlocks = 737.2619
You must have an INTEGER number of blocks, otherwise there are some elements "left over". Why do you not have an integer number of blocks?
How many elements do you want in each block?
chris pamp
chris pamp 2022년 9월 10일
Thank you for your answer. I want 500 elements to each block. To have leftovers is not a problem
Image Analyst
Image Analyst 2022년 9월 10일
편집: Image Analyst 2022년 9월 10일
Did you try something obvious like a for loop?
counter = 1;
for k = 1 : 500 : length(yourVector)
k2 = min([k + 499, length(yourVector)]);
theMeans(counter) = mean(yourVector(k : k2));
counter = counter + 1;
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2018a

질문:

2018년 9월 18일

편집:

2022년 9월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by