MATLAB Answers

## for loop to matrix calculation

Asked by Andrea Giordano

### Andrea Giordano (view profile)

on 3 Dec 2018
Latest activity Commented on by Jan

### Jan (view profile)

on 4 Dec 2018
Accepted Answer by Jan

### Jan (view profile)

Hello there,
I do not know how to optimize this calculation. It is very slow, since vector "my_vector" is made of 3 millions indeces.
I would like to perform the same calculation using matrices, but I really do not know how to do it!
Thank you!
Have a good one,
Andrea.
corr = zeros(1,n_A*p_A-1); % row vector of 3762799
v = residual(my_vector);
variance = mean(v.^2);
index_corr = 0;
ref_length = 0;
for l = 1:(n_A*p_A-1)
for j = 1:(n_A*p_A-l)
corr(1,l) = corr(1,l)+v(1,j)*v(1,j+l);
end
corr(1,l) = corr(1,l)/((n_A*p_A-l)*variance);
if (corr(1,l) < (1/exp(1)))
if index_corr == 0
ref_length = l;
end
index_corr = 1;
end
end

Jan

### Jan (view profile)

on 3 Dec 2018
It would be useful if you explain, what the code should do. Some meaningful comments in the code are required also to improve its quality. Please provide some meaningful inputs also, because this is more efficient than letting the readers guess the inputs.

Sign in to comment.

## 1 Answer

Answer by Jan

on 3 Dec 2018
Edited by Jan

### Jan (view profile)

on 3 Dec 2018
Accepted Answer

1/exp(1) is a very expensive calculation. Do this once before the loop.
The index "l" (lowercase L) looks like a "1", so I replaced it by "k".
corr = zeros(1, n_A*p_A-1); % row vector of 3762799
v = residual(my_vector);
variance = mean(v .^ 2);
index_corr = 0;
ref_length = 0;
c = 1 / exp(1);
for k = 1:(n_A*p_A-1)
n = n_A * p_A - k;
% Use DOT product to calculate the sum:
corr(1, k) = corr(1, k) + v(1, 1:n) * v(1, (1 + k):(n + k)).';
% Alternative - assumed to be slower:
% corr(1, k) = corr(1, k) + sum(v(1, 1:n) .* v(1, (1 + k):(n + k)));
corr(1, k) = corr(1,k) / ((n_A*p_A-k) * variance);
if corr(1,k) < c
if index_corr == 0
ref_length = k;
end
index_corr = 1;
end
end

Andrea Giordano

### Andrea Giordano (view profile)

on 3 Dec 2018
Okay, thak you for the advice!
There you go with teh comments!
corr = zeros(1, 3762799); % row vector of 3762799
v = residual(my_vector); % calculating the residual of a 'double' vector
variance = mean(v .^ 2); % calculating the variance of 'my_vector'
index_corr = 0;
ref_length = 0;
c = 1 / exp(1);
for k = 1:(n_A*p_A-1) % calculates the correlation function of the set C(k)
n = n_A * p_A - k;
% Use DOT product to calculate the sum:
corr(1, k) = corr(1, k) + v(1, 1:n) * v(1, (1 + k):(n + k)).';
% Alternative - assumed to be slower:
% corr(1, k) = corr(1, k) + sum(v(1, 1:n) .* v(1, (1 + k):(n + k)));
corr(1, k) = corr(1,k) / ((n_A*p_A-k) * variance);
if corr(1,k) < c
if index_corr == 0
ref_length = k;
end
index_corr = 1;
end
end
Andrea Giordano

### Andrea Giordano (view profile)

on 4 Dec 2018
Hello Jan,
thank you again for your kind response!
I get an error though (implementing dot product), and I do not know how to fix it!
Index in position 2 exceeds array bounds (must not exceed 1).
Error in example2 (line 67)
corr(1, k) = corr(1, k) + v(1, 1:n) * v(1, (1 + k):(n + k)).';
Jan

### Jan (view profile)

on 4 Dec 2018
@Andrea: Unfortunately I cannot run your code, because you did not provide the input arguments. Then you have to debug the code by your own. Perhaps one of the inputs is a column vector? Try this:
...
v = V(:).';
for k = 1:(n_A*p_A-1)
n = n_A * p_A - k;
corr(k) = corr(k) + v(1:n) * v((1 + k):(n + k)).';
corr(k) = corr(k) / ((n_A*p_A-k) * variance);
if corr(k) < c
if index_corr == 0
ref_length = k;
end
index_corr = 1;
end
end

Sign in to comment.