I have the for loop (outlined below) in my code which takes a while to run. CALC is a function I have defined; Dis a matrix; Y is a matrix; k is a vector. Is there a way I can vectorize this code such that I do away with the for loop? Any contribution will be highly appreciated.
for column = 1:n
q(:,column) = CALC(D,Y(:,column), k(column));
end
The CALC function is outlined below:
function [x] = CALC(A, y, s)
[m, n] = size(A);
% y is an m x 1 vector
% s is an integer
r = y;
index_cols = [];
atoms = [];
for i = 1 : s
[max_r, lambda_t] = max(abs(r'*A));
index_cols = [index_cols, lambda_t];
atoms = [atoms, A(:,lambda_t)];
x_t = pinv(atoms)*y;
r = y - atoms*x_t;
end
x = zeros(n,1);
x(index_cols) = x_t;
end
CALC
does. It's like you're giving us a black box, where we don't know how it actually works and you're telling us to make this black box run faster. It's impossible to know how to make it faster unless we understand what you're doing. – rayryeng