How can I Vectorize this For Loop in MATLAB Code?

2019-09-02 18:55发布

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

1条回答
时光不老,我们不散
2楼-- · 2019-09-02 19:44

I will expand on rayryeng's comment. Vectorization means grouping some elementary operations together in such a way that they can be jointly handled by a low-level routine. But the bulk of execution time of your code is the computation of pinv(atoms); everything else is not nearly as expensive.

  • If your task is to saw several pieces of wood, you can clamp them together and saw them all at once. That's vectorization.
  • But that does not work when you're a mechanic whose task is to repair several cars. The bulk of your time will have to be spent working on an individual car.

Things you can consider:

  1. Caching. Your code computes pseudoinverses of matrices that are always made of the columns of the same matrix D. So it may happen to call pinv with the same atoms input multiple times. Investigate whether this happens often enough to warrant caching the pseudoinverses. Here's an example of caching Matlab results

  2. Parallelizing, if you have the hardware and software for this.

  3. Rethink the algorithm...

查看更多
登录 后发表回答