Problem relating to mathematical expression
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
Are Mjaavatten
el 28 de Mayo de 2020
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);
1 comentario
Tino
el 1 de Jun. de 2020
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!