0
votes

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
What does CALC actually do ? It would help in giving ideas about how to vectorize.paisanco
We can't vectorize this code unless we know what 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
Thanks for your contribution @paisanco I have updated the question to include the CALC functionAmanda
Thanks for your contribution @rayryeng I have updated the question to include the CALC functionAmanda
I see the pseudo inverse. Using that already makes your code non vectorizable.rayryeng

1 Answers

3
votes

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...