Problem relating to mathematical expression

조회 수: 1 (최근 30일)
Tino
Tino 2020년 5월 28일
댓글: Tino 2020년 6월 1일
Hello I have a set of 10 random variables using a window length of k =5
0.1, 0.2, 0.4, 0.5, 0.4, 0.6, 0.6, 0.6, 0.2, 0.3
first window size ( 0.1, 0.2 ,0.4, 0.5, 0.4)
first answer = (0.1 + 0.2 + 0.4 + 0.5 + 0.4)/5 = 0.32
second answer =( 0.2 + 0.4 + 0.5 + 0.4 )/4 = 0.375
third answer = (0.4 + 0.5 + 0.4)/3 = 1.3
fourth answer = (0.5 + 0.4)/2 =0.45
fifth answer = 0.4/1 = 0.4
second window size (0.6,0.6, 0.6, 0.2, 0.3)
first answer = 0.6 + 0.6 + 0.6 + 0.2 + 0.3 /5 = 0.46
second answer = 0.6 + 0.6 + 0.2 + 0.3/4 = 0.425
third answer = 0.6 + 0.2 + 0.3/3 = 0.367
fourth answer = 0.2 + 0.3 /2 = 0.25
fifth answer = 0.3/1 =0.3
How do I compute this process in matlab
Your response will be greatly appreciated
total answer is then 0.32, 0.375, 1.3, 0.45, 0.4 , 0.46, 0.425, 0.367, 0.25, 0.3
if the last remaining number is not up to the window size then we use the total number remaining as the window size.
for instance we have 4 4 4 4 2 2 2
if we take the window length of 5 (4 4 4 4 2)
the remain ( 2, 2) will be computed using the window length of 2
thanks in advance

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 28일
편집: Ameer Hamza 2020년 5월 28일
Try this
x = [0.1, 0.2, 0.4, 0.5, 0.4, 0.6, 0.6, 0.6, 0.2, 0.3];
n = numel(x);
k = 5;
if round(n/k)==n/k
split = mat2cell(x, 1, repmat(k, 1, floor(numel(x)/k)));
else
split = mat2cell(x, 1, [repmat(k, 1, floor(numel(x)/k)) mod(n, k)]);
end
helpFun = @(x) {fliplr(cumsum(fliplr(x))./(1:numel(x)))};
split_mean = cellfun(helpFun, split);
split_mean = [split_mean{:}]
  댓글 수: 1
Tino
Tino 2020년 6월 1일
Thank you Ameer. Greatly appreciated

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

추가 답변 (1개)

Are Mjaavatten
Are Mjaavatten 2020년 5월 28일
Ameer's answer is correct, of course. Here is another approach, without the elegant Matlab functions:
N = 5; % Max window size
% Throw in a few extra elements, to test:
x = [0.1, 0.2, 0.4, 0.5, 0.4, 0.6, 0.6, 0.6, 0.2, 0.3, 0.5, 0.3];
L = length(x);
result = zeros(1,L);
last = N;
first = 0;
while first < L
while last > first
first = first + 1;
result(first) = sum(x(first:last))/(last-first + 1);
end
last = min(last + N, L);
end
disp(result);

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by