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:
vectorization advice

Subject: vectorization advice

From: Dick Startz

Date: 4 Apr, 2012 14:09:29

Message: 1 of 9

I have an inner loop, called many times, that looks something like
this.
n = 500;
k = 3;
e = randn(k,n); % not the real data, but a typical size
tempSum = zeros(k,k);
        for i=1:n
            tempSum = tempSum + e(:,i)*(e(:,i)');
        end

Any way to vectorize this? The key is that n is much larger than k.
-Dick Startz

Subject: vectorization advice

From: Matt J

Date: 4 Apr, 2012 14:21:11

Message: 2 of 9

tempSum=e*e';

Subject: vectorization advice

From: Dick Startz

Date: 4 Apr, 2012 15:04:59

Message: 3 of 9

On Wed, 4 Apr 2012 14:21:11 +0000 (UTC), "Matt J "
<mattjacREMOVE@THISieee.spam> wrote:

>tempSum=e*e';

Sigh....
Well that's obvious. I have no explanation of why I didn't do it this
way in the first place.

You've made my morning faster by an order of magnitude.

Thanks, Matt.

Subject: vectorization advice

From: Dick Startz

Date: 5 Apr, 2012 22:18:18

Message: 4 of 9

Several days ago, Matt J. was nice enough to give me some
vectorization advice. I now have a somewhat more complicated problem

I have an inner loop, called many times, that looks something like
this.
n = 500;
k = 3;
e = randn(k,n); % not the real data, but a typical size
H = randn(k,k); % not the real data either
tempSum = zeros(k,k);
        for i=1:n
            tempSum = tempSum + e(:,i)*H*(e(:,i)');
        end

Any way to vectorize this? The key is that n is much larger than k.

[In the original problem, H wasn't there and Matt J. pointed out the
obvious answer, tempSum=e*e';]


-Dick Startz

Subject: vectorization advice

From: Roger Stafford

Date: 6 Apr, 2012 02:28:37

Message: 5 of 9

Dick Startz <@> wrote in message <jc6sn7dlgfvqrre43nj444h3s189hlf3gk@4ax.com>...
> for i=1:n
> tempSum = tempSum + e(:,i)*H*(e(:,i)');
> end
- - - - - - - - - -
  As it stands, your for-loop wouldn't work. You are multiplying e(:,i) by H, but e(:,i) has only one column and H has 3 rows. Matlab will complain. The same difficulty occurs in H*e(:,i)' with 3 columns and 1 row, resp. Perhaps you meant to write it a little differently but I can't figure out what that might have been.

Roger Stafford

Subject: vectorization advice

From: Dick Startz

Date: 6 Apr, 2012 03:00:03

Message: 6 of 9

On Fri, 6 Apr 2012 02:28:37 +0000 (UTC), "Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote:

>Dick Startz <@> wrote in message <jc6sn7dlgfvqrre43nj444h3s189hlf3gk@4ax.com>...
>> for i=1:n
>> tempSum = tempSum + e(:,i)*H*(e(:,i)');
>> end
>- - - - - - - - - -
> As it stands, your for-loop wouldn't work. You are multiplying e(:,i) by H, but e(:,i) has only one column and H has 3 rows. Matlab will complain. The same difficulty occurs in H*e(:,i)' with 3 columns and 1 row, resp. Perhaps you meant to write it a little differently but I can't figure out what that might have been.
>
>Roger Stafford

You're right of course. My fault for trying to simplify my problem
without being careful enough. Let me try again.

n = 500;
q=4;
k = 3;
e = randn(q,k,n); % not the real data, but a typical size
H = randn(k,k); % not the real data either
tempSum = zeros(q,q);
        for i=1:n
            tempSum = tempSum + e(:,:,i)*H*(e(:,:,i)');
        end

Subject: vectorization advice

From: Roger Stafford

Date: 6 Apr, 2012 04:05:27

Message: 7 of 9

Dick Startz <@> wrote in message <ksmsn7509sa72jcu2kf89qpi03n1hvj9ku@4ax.com>...
> n = 500;
> q=4;
> k = 3;
> e = randn(q,k,n); % not the real data, but a typical size
> H = randn(k,k); % not the real data either
> tempSum = zeros(q,q);
> for i=1:n
> tempSum = tempSum + e(:,:,i)*H*(e(:,:,i)');
> end
- - - - - - - - - -
  This is not vectorized but you might try it. It uses matrix multiplication along the 500-element dimension. It is more or less an extension of Matt's suggestion.

 t = zeros(q);
 for ix = 1:k
  for jx = 1:k
   t = t + h(ix,jx)*(squeeze(e(:,ix,:))*squeeze(e(:,jx,:))');
  end
 end
 
Roger Stafford

Subject: vectorization advice

From: Bruno Luong

Date: 6 Apr, 2012 06:37:16

Message: 8 of 9


> n = 500;
> q=4;
> k = 3;
> e = randn(q,k,n); % not the real data, but a typical size
> H = randn(k,k); % not the real data either
> tempSum = zeros(q,q);
> for i=1:n
> tempSum = tempSum + e(:,:,i)*H*(e(:,:,i)');
> end

Here is a vectorized code, using James's mtimesx
http://www.mathworks.com/matlabcentral/fileexchange/25977

E = reshape(permute(e, [2 1 3]), k, []);
A = reshape(H*E, [k q n]);
Sum = sum(mtimesx(e, A),3)

% Bruno

Subject: vectorization advice

From: Dick Startz

Date: 7 Apr, 2012 01:09:30

Message: 9 of 9

On Fri, 6 Apr 2012 06:37:16 +0000 (UTC), "Bruno Luong"
<b.luong@fogale.findmycountry> wrote:

>
>> n = 500;
>> q=4;
>> k = 3;
>> e = randn(q,k,n); % not the real data, but a typical size
>> H = randn(k,k); % not the real data either
>> tempSum = zeros(q,q);
>> for i=1:n
>> tempSum = tempSum + e(:,:,i)*H*(e(:,:,i)');
>> end
>
>Here is a vectorized code, using James's mtimesx
>http://www.mathworks.com/matlabcentral/fileexchange/25977
>
>E = reshape(permute(e, [2 1 3]), k, []);
>A = reshape(H*E, [k q n]);
>Sum = sum(mtimesx(e, A),3)
>
>% Bruno

Bruno, Roger:

Thanks to both of you. This is what I needed.
-Dick Startz

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