Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to vectorize the following for-loops (gaussian low pass filter and Vandermonde matrix construction)

Subject: how to vectorize the following for-loops (gaussian low pass filter and Vandermonde matrix construction)

From: Tanya Rosenblit

Date: 15 Dec, 2010 13:04:06

Message: 1 of 5

Can the following for-loops be vectorized?

for i=1:np
    w = exp( -(lx-lx(i)).^2 / (2*width.^2) ); % a 'Gaussian' window
    ww = find(w > 0.03); % truncate the gaussian, run faster
    %s(i)=length(ww);
    res = polyfitw(lx(ww),ly(ww),w(ww),2);
    f2(i) = exp(res(3) + res(2)*lx(i) + res(1)*(lx(i)^2));
    df(i) = res(2)+(2*res(1)*lx(i));
    ddf(i) = 2*res(1);
end



% Construct Vandermonde matrix
V(:,n+1) = ones(length(x),1,class(x));
for j = n:-1:1
   V(:,j) = x.*V(:,j+1);
end

Thanks in advance,
Tanya

Subject: how to vectorize the following for-loops (gaussian low pass filter and Vandermonde matrix construction)

From: Sandro

Date: 15 Dec, 2010 21:09:07

Message: 2 of 5

% Construct Vandermonde matrix

(repmat(x(:),1,length(x))).^repmat(length(x)-1:-1:0,length(x),1)

Subject: how to vectorize the following for-loops (gaussian low pass filter and Vandermonde matrix construction)

From: Jan Simon

Date: 16 Dec, 2010 12:16:05

Message: 3 of 5

Dear Tanya,

> for i=1:np
> w = exp( -(lx-lx(i)).^2 / (2*width.^2) ); % a 'Gaussian' window
> ww = find(w > 0.03); % truncate the gaussian, run faster
> %s(i)=length(ww);
> res = polyfitw(lx(ww),ly(ww),w(ww),2);
> f2(i) = exp(res(3) + res(2)*lx(i) + res(1)*(lx(i)^2));
> df(i) = res(2)+(2*res(1)*lx(i));
> ddf(i) = 2*res(1);
> end

I would not vectorize it, but use the standard methods for acceleration at first:
1. Move all calculations, which do not change inside the loop, to outside the loop. E.g. "2*width.^2" is constant.
2. Omit FIND, if logical indexing works as well.
3. What is polyfitw? If it calls Matlab's toolbox function POLYFIT, this can be accelerated dramatically by inlining just the needed lines from POLYFIT. The input checks and suppressed WARNING in POLYFIT use more than 75% of the computing time (depending on the size of the inputs). And the repeated QR decomposition of the Vandermonde matrix wastes even more time.
If polyfitw comes from http://www.physics.mcgill.ca/~kilfoil , this could be improved remarkably also:
  coorm^-1; % Absolutely useless
  p = coorm^-1 * b; % No efficient method to solve a matrix equation

Kind regards, Jan

Subject: how to vectorize the following for-loops (gaussian low pass filter and Vandermonde matrix construction)

From: Tanya Rosenblit

Date: 20 Dec, 2010 20:27:09

Message: 4 of 5

Thank you Sandro,

Much better now! :)

Best regards,
Tanya

Subject: how to vectorize the following for-loops (gaussian low pass filter and Vandermonde matrix construction)

From: proecsm

Date: 20 Dec, 2010 23:23:15

Message: 5 of 5

"Sandro " <smgoni@unq.edu.ar> wrote in message <iebapj$ati$1@fred.mathworks.com>...
> % Construct Vandermonde matrix
>
> (repmat(x(:),1,length(x))).^repmat(length(x)-1:-1:0,length(x),1)

another way:

vander(x)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us