How can I Vectorize this For Loop in MATLAB Code? -


i have loop (outlined below) in code takes while run. calc function have defined; dis matrix; y matrix; k vector. there way can vectorize code such away loop? contribution highly appreciated.

for column = 1:n     q(:,column) = calc(d,y(:,column), k(column)); end 

the calc function outlined below:

function [x] = calc(a, y, s)  [m, n] = size(a);  % y m x 1 vector % s integer  r = y;  index_cols = []; atoms      = [];  = 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 

i expand on rayryeng's comment. vectorization means grouping elementary operations in such way can jointly handled low-level routine. bulk of execution time of code computation of pinv(atoms); else not expensive.

  • if task saw several pieces of wood, can clamp them , saw them @ once. that's vectorization.
  • but not work when you're mechanic task repair several cars. bulk of time have spent working on individual car.

things can consider:

  1. caching. code computes pseudoinverses of matrices made of columns of same matrix d. may happen call pinv same atoms input multiple times. investigate whether happens enough warrant caching pseudoinverses. here's example of caching matlab results

  2. parallelizing, if have hardware , software this.

  3. rethink algorithm...


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -