# Multiple summations in one formula

18 views (last 30 days)
Inti Vanmechelen on 10 Sep 2019
Commented: darova on 10 Sep 2019 Hi all,
I'm not very good at matlab and trying incorporate the above formula. I'm trying to do part (2) and (3) first, to get them more easily in the complete formula.
I am struggling with (3): Y is in my case a 101x14 matrix and I think the mean should then be one number. I can obviously take the mean of the columns and rows seperately, yielding a 101x1 and a 14x1 vector, but these are obvousily not compatible for matrix multiplication.
Any advice on how to make this work better?
M = 1; % Test day
N = 14; % Number of runs (or repetitions)
T = 101; % Number of time points
for n = 1:N
for t = 1:T
R2 = ....
end
end

Thales on 10 Sep 2019
You can use for loops to do the sum or use the sum function:
clc; clear; close all force;
M = 1; % Test day
N = 14; % Number of runs (or repetitions)
T = 101; % Number of time points
Y = rand(M,N,T); % random data to illustrate
% using for loops
Yi_for = zeros(M,1);
for i=1:M
for j=1:N
for t=1:T
Yi_for(i) = Yi_for(i) + Y(i,j,t);
end
end
Yi_for(i) = Yi_for(i)/N/T;
end
% using sums
Yi_sum = zeros(M,1);
for i=1:M
Yi_sum(i) = sum(sum(Y,2),3)/N/T;
end
You can expand this to calculate the average at time point t on the ith test day:
% Yit should be a matrix
Yit = zeros(M,T);
for i=1:M
for t=1:T
for j=1:N
Yit(i,t) = Yit(i,t)+Y(i,j,t);
end
Yit(i,t) = Yit(i,t)/N;
end
end
With Yi and Yit previously calculated, you can define Ra (I splitted the calculations of the numerator and denominator of the fraction to make it clearer):
% Ra
Yi = Yi_sum;
Ra = 1;
% partial sums
num = 0; % numerator
for i=1:M
for j=1:N
for t=1:T
num = num + (Y(i,j,t)-Yit(i,t))^2/M/T/(N-1);
end
end
end
den = 0; % denominator
for i=1:M
for j=1:N
for t=1:T
den = den + (Y(i,j,t)-Yi(i))^2/M/(N*T-1);
end
end
end
Ra2 = 1-num/den

Inti Vanmechelen on 10 Sep 2019
Thank you for the stepswise explanation!
This well definetely get me where I need to be, many thanks :)
darova on 10 Sep 2019
By my answer is better :(

darova on 10 Sep 2019
Shorter version
clc,clear
Yit = repmat( mean(Yijt,2), [1 N 1] ); % get mean and make 3D matrix
Yi = mean(mean(Yijt,2),3); % get mean 2d and 3d dimensions
Yi = repmat( Yi, [1 N T] ); % make 3D matrix
upsum = (Yijt-Yit).^2;
botsum = (Yijt-Yi).^2;
R2a = 1 - sum(upsum(:))/sum(botsum(:)) * T*(N-1)/(N*T-1);

#### 1 Comment

Inti Vanmechelen on 10 Sep 2019
Hi Darova,
Since I am still trying get my head around implementing stuff from other in matlab, the long answer helped me a bit better to understand all the steps :) But thank you for the reply!