Got Questions? Get Answers.
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:
More vectorization help

Subject: More vectorization help

From: Dick Startz

Date: 10 Apr, 2012 00:40:17

Message: 1 of 5

I recently asked for some help with vectorization and received very
helpful answers. Now I have what I thought would only be a very slight
twist...but I'm not seeing how to do it. Here's the Matlab program.

n = 500;
q=6;
k = 2;
X = randn(k,q,n);
H = randn(k,k);
result = zeros(q,q);

for i=1:n
    result = result + squeeze(X(:,:,i))'*H*squeeze(X(:,:,i));
end

result

Guidance would be much appreciated.
-Dick Startz

Subject: More vectorization help

From: Bruno Luong

Date: 10 Apr, 2012 07:29:20

Message: 2 of 5

Dick Startz <@> wrote in message <4607o7d2jscoen21hf7l227n1lt24d1rrh@4ax.com>...
> I recently asked for some help with vectorization and received very
> helpful answers. Now I have what I thought would only be a very slight
> twist...but I'm not seeing how to do it. Here's the Matlab program.
>
> n = 500;
> q=6;
> k = 2;
> X = randn(k,q,n);
> H = randn(k,k);
> result = zeros(q,q);
>
> for i=1:n
> result = result + squeeze(X(:,:,i))'*H*squeeze(X(:,:,i));
> end
>
> result
>
> Guidance would be much appreciated.

This is ALMOST like before. If you permute the first and second dimension of X, it will be like before.

Sum = sum(mtimesx(permute(X, [2 1 3]), reshape(H*reshape(X, k, []), [k q n])),3)

Bruno

Subject: More vectorization help

From: Dick Startz

Date: 10 Apr, 2012 13:36:49

Message: 3 of 5

On Tue, 10 Apr 2012 07:29:20 +0000 (UTC), "Bruno Luong"
<b.luong@fogale.findmycountry> wrote:

>Dick Startz <@> wrote in message <4607o7d2jscoen21hf7l227n1lt24d1rrh@4ax.com>...
>> I recently asked for some help with vectorization and received very
>> helpful answers. Now I have what I thought would only be a very slight
>> twist...but I'm not seeing how to do it. Here's the Matlab program.
>>
>> n = 500;
>> q=6;
>> k = 2;
>> X = randn(k,q,n);
>> H = randn(k,k);
>> result = zeros(q,q);
>>
>> for i=1:n
>> result = result + squeeze(X(:,:,i))'*H*squeeze(X(:,:,i));
>> end
>>
>> result
>>
>> Guidance would be much appreciated.
>
>This is ALMOST like before. If you permute the first and second dimension of X, it will be like before.
>
>Sum = sum(mtimesx(permute(X, [2 1 3]), reshape(H*reshape(X, k, []), [k q n])),3)
>
>Bruno

Bruno:
The (repeated) help is much appreciated. I didn't use your mtimesx
solution before because the documentation says mtimesx requires a C
compiler and I want to be able to distribute the code without
complication. (I don't regard requiring the user to get mtimesx to
itself be a complication.)

I see that mtimesx has a number of "modes." (By the way, your 2009
contribution on the FileExchange was quite helpful.) Do any of the
modes eliminate the need for the C compiler? Or if not, is there
another way to vectorize?

Thanks.
Dick Startz

Subject: More vectorization help

From: Bruno Luong

Date: 11 Apr, 2012 06:38:12

Message: 4 of 5

Dick Startz <@> wrote in message <dhd8o71n7hkrvi13j0l4siiuqf0jc0sjro@4ax.com>...
> On Tue, 10 Apr 2012 07:29:20 +0000 (UTC), "Bruno Luong"
> <b.luong@fogale.findmycountry> wrote:
>
> >Dick Startz <@> wrote in message <4607o7d2jscoen21hf7l227n1lt24d1rrh@4ax.com>...
> >> I recently asked for some help with vectorization and received very
> >> helpful answers. Now I have what I thought would only be a very slight
> >> twist...but I'm not seeing how to do it. Here's the Matlab program.
> >>
> >> n = 500;
> >> q=6;
> >> k = 2;
> >> X = randn(k,q,n);
> >> H = randn(k,k);
> >> result = zeros(q,q);
> >>
> >> for i=1:n
> >> result = result + squeeze(X(:,:,i))'*H*squeeze(X(:,:,i));
> >> end
> >>
> >> result
> >>
> >> Guidance would be much appreciated.
> >
> >This is ALMOST like before. If you permute the first and second dimension of X, it will be like before.
> >
> >Sum = sum(mtimesx(permute(X, [2 1 3]), reshape(H*reshape(X, k, []), [k q n])),3)
> >
> >Bruno
>
> Bruno:
> The (repeated) help is much appreciated. I didn't use your mtimesx
> solution before because the documentation says mtimesx requires a C
> compiler and I want to be able to distribute the code without
> complication. (I don't regard requiring the user to get mtimesx to
> itself be a complication.)
>
> I see that mtimesx has a number of "modes." (By the way, your 2009
> contribution on the FileExchange was quite helpful.) Do any of the
> modes eliminate the need for the C compiler? Or if not, is there
> another way to vectorize?

You can replace mtimesx by pure Matlab code:
http://www.mathworks.com/matlabcentral/fileexchange/24260
 
Sum = sum(SliceMultiProd(permute(X, [2 1 3]), reshape(H*reshape(X, k, []), [k q n])),3)

% Bruno

Subject: More vectorization help

From: Dick Startz

Date: 11 Apr, 2012 23:44:17

Message: 5 of 5

On Wed, 11 Apr 2012 06:38:12 +0000 (UTC), "Bruno Luong"
<b.luong@fogale.findmycountry> wrote:

>Dick Startz <@> wrote in message <dhd8o71n7hkrvi13j0l4siiuqf0jc0sjro@4ax.com>...
>> On Tue, 10 Apr 2012 07:29:20 +0000 (UTC), "Bruno Luong"
>> <b.luong@fogale.findmycountry> wrote:
>>
>> >Dick Startz <@> wrote in message <4607o7d2jscoen21hf7l227n1lt24d1rrh@4ax.com>...
>> >> I recently asked for some help with vectorization and received very
>> >> helpful answers. Now I have what I thought would only be a very slight
>> >> twist...but I'm not seeing how to do it. Here's the Matlab program.
>> >>
>> >> n = 500;
>> >> q=6;
>> >> k = 2;
>> >> X = randn(k,q,n);
>> >> H = randn(k,k);
>> >> result = zeros(q,q);
>> >>
>> >> for i=1:n
>> >> result = result + squeeze(X(:,:,i))'*H*squeeze(X(:,:,i));
>> >> end
>> >>
>> >> result
>> >>
>> >> Guidance would be much appreciated.
>> >
>> >This is ALMOST like before. If you permute the first and second dimension of X, it will be like before.
>> >
>> >Sum = sum(mtimesx(permute(X, [2 1 3]), reshape(H*reshape(X, k, []), [k q n])),3)
>> >
>> >Bruno
>>
>> Bruno:
>> The (repeated) help is much appreciated. I didn't use your mtimesx
>> solution before because the documentation says mtimesx requires a C
>> compiler and I want to be able to distribute the code without
>> complication. (I don't regard requiring the user to get mtimesx to
>> itself be a complication.)
>>
>> I see that mtimesx has a number of "modes." (By the way, your 2009
>> contribution on the FileExchange was quite helpful.) Do any of the
>> modes eliminate the need for the C compiler? Or if not, is there
>> another way to vectorize?
>
>You can replace mtimesx by pure Matlab code:
>http://www.mathworks.com/matlabcentral/fileexchange/24260
>
>Sum = sum(SliceMultiProd(permute(X, [2 1 3]), reshape(H*reshape(X, k, []), [k q n])),3)
>
>% Bruno

I put SliceMultiProd into my program, replacing a for loop. The speed
of the entire program roughly tripled!
Thanks Bruno!
-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