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:
Repmat and Matrix Multiplication

Subject: Repmat and Matrix Multiplication

From: Eric W

Date: 31 Mar, 2009 19:24:01

Message: 1 of 7

Hi,

I'm looking for a better way to do this:

data = permute(rand(3,3,500),[2 1 3])
trans = repmat(rand(3,3),[1 1 length(data)])

answer = trans .* data

without using repmat (or is repmat the fastest way to do this) or permute (same question).

Subject: Repmat and Matrix Multiplication

From: Peter Boettcher

Date: 31 Mar, 2009 19:35:35

Message: 2 of 7

"Eric W" <gmail@ewilliams2006.com> writes:

> I'm looking for a better way to do this:
>
> data = permute(rand(3,3,500),[2 1 3])
> trans = repmat(rand(3,3),[1 1 length(data)])
>
> answer = trans .* data
>
> without using repmat (or is repmat the fastest way to do this) or
> permute (same question).

What's the permute for? Permuting random data 3x3x500 to 3x3x500 does
nothing.

bsxfun is what you're looking for for the multiply.

-Peter

Subject: Repmat and Matrix Multiplication

From: Eric W

Date: 31 Mar, 2009 19:44:01

Message: 3 of 7

Peter Boettcher <boettcher@ll.mit.edu> wrote in message <muyr60d8ozc.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "Eric W" <gmail@ewilliams2006.com> writes:
>
> > I'm looking for a better way to do this:
> >
> > data = permute(rand(3,3,500),[2 1 3])
> > trans = repmat(rand(3,3),[1 1 length(data)])
> >
> > answer = trans .* data
> >
> > without using repmat (or is repmat the fastest way to do this) or
> > permute (same question).
>
> What's the permute for? Permuting random data 3x3x500 to 3x3x500 does
> nothing.
>
> bsxfun is what you're looking for for the multiply.
>
> -Peter

Provided is a general idea of the matrices I'm working with, permuting the actual data in this manner (swapping rows and columns inside their respective pages) makes sense.

I'll look at bsxfun for the multiplication. Thank you.

Subject: Repmat and Matrix Multiplication

From: Eric W

Date: 1 Apr, 2009 16:05:19

Message: 4 of 7

"Eric W" <gmail@ewilliams2006.com> wrote in message <gqtrq1$q76$1@fred.mathworks.com>...
> Peter Boettcher <boettcher@ll.mit.edu> wrote in message <muyr60d8ozc.fsf@G99-Boettcher.llan.ll.mit.edu>...
> > "Eric W" <gmail@ewilliams2006.com> writes:
> >
> > > I'm looking for a better way to do this:
> > >
> > > data = permute(rand(3,3,500),[2 1 3])
> > > trans = repmat(rand(3,3),[1 1 length(data)])
> > >
> > > answer = trans .* data
> > >
> > > without using repmat (or is repmat the fastest way to do this) or
> > > permute (same question).
> >
> > What's the permute for? Permuting random data 3x3x500 to 3x3x500 does
> > nothing.
> >
> > bsxfun is what you're looking for for the multiply.
> >
> > -Peter
>
> Provided is a general idea of the matrices I'm working with, permuting the actual data in this manner (swapping rows and columns inside their respective pages) makes sense.
>
> I'll look at bsxfun for the multiplication. Thank you.

I've tried BSXFUN, but I can't seem to get it to work correctly.

data = rand(3,3,5000);
tform = rand(3,3);

testResult = bsxfun(@times,tform,data);

for i=1:length(data)
trueResult(:,:,i)= tform * data(:,:,i);
end;

How can i get bsxfun to mimic that for loop? Or any other vectorized implementation?

Subject: Repmat and Matrix Multiplication

From: Bruno Luong

Date: 1 Apr, 2009 16:57:01

Message: 5 of 7

"Eric W" <gmail@ewilliams2006.com> wrote in message <gr03bv$n2j$1@fred.mathworks.com>...

>
> testResult = bsxfun(@times,tform,data);
>

bsxfun can replaces repmat, but you still need to permute the matrix.

Bruno

Subject: Repmat and Matrix Multiplication

From: Eric W

Date: 1 Apr, 2009 16:58:01

Message: 6 of 7

"Eric W" <gmail@ewilliams2006.com> wrote in message <gr03bv$n2j$1@fred.mathworks.com>...
> "Eric W" <gmail@ewilliams2006.com> wrote in message <gqtrq1$q76$1@fred.mathworks.com>...
> > Peter Boettcher <boettcher@ll.mit.edu> wrote in message <muyr60d8ozc.fsf@G99-Boettcher.llan.ll.mit.edu>...
> > > "Eric W" <gmail@ewilliams2006.com> writes:
> > >
> > > > I'm looking for a better way to do this:
> > > >
> > > > data = permute(rand(3,3,500),[2 1 3])
> > > > trans = repmat(rand(3,3),[1 1 length(data)])
> > > >
> > > > answer = trans .* data
> > > >
> > > > without using repmat (or is repmat the fastest way to do this) or
> > > > permute (same question).
> > >
> > > What's the permute for? Permuting random data 3x3x500 to 3x3x500 does
> > > nothing.
> > >
> > > bsxfun is what you're looking for for the multiply.
> > >
> > > -Peter
> >
> > Provided is a general idea of the matrices I'm working with, permuting the actual data in this manner (swapping rows and columns inside their respective pages) makes sense.
> >
> > I'll look at bsxfun for the multiplication. Thank you.
>
> I've tried BSXFUN, but I can't seem to get it to work correctly.
>
> data = rand(3,3,5000);
> tform = rand(3,3);
>
> testResult = bsxfun(@times,tform,data);
>

>
> How can i get bsxfun to mimic that for loop? Or any other vectorized implementation?


For anyone looking at this thread, it appears that bsxfun is doing exactly what the description says it's supposed to do (Apply <<<element-by-element>>> binary operation to two arrays with singleton expansion enabled):

data = rand(3,3,5000);
tform = rand(3,3);

tic
testResult = bsxfun(@times,tform,data);
toc

tic
for i=1:length(data)
  trueResult(:,:,i)= tform * data(:,:,i);
end;
toc

tic
for i=1:length(TUS2N_2HZ)
test2Result(:,:,i) = TUP2A .* TUS2N_2HZ(:,:,i);
end;
toc

diff1 = testResult - trueResult
diff2 = test2Result - trueResult

found_problem = diff1 - diff2

I'd still like a way of vectorizing the non element-by-element loop, but at least I don't feel crazy now.

Subject: Repmat and Matrix Multiplication

From: Bruno Luong

Date: 1 Apr, 2009 17:32:02

Message: 7 of 7

"Eric W" <gmail@ewilliams2006.com> wrote in message <gr06ep$a4m$1@fred.mathworks.com>...

>
> I'd still like a way of vectorizing the non element-by-element loop, but at least I don't feel crazy now.

But that is NOT what you are described at post #1 (element by element multiplication). If you want to solve another problem, it is better to open a new thread, or even better: try post the correct problem from the start (I much prefer the later).

Anyway, back to the problem, please see this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/247334

Bruno

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