Thread Subject: SIMPLE way to multiply matrix array with vector?

Subject: SIMPLE way to multiply matrix array with vector?

From: Volker Klink

Date: 5 Jun, 2008 12:02:06

Message: 1 of 5

Hi all!

I wondered if there's -simple- a way to multiply the
elements of a 1-by-n vector with a m-by-n-by-p matrix array?

Here's an example what I want to do:

a = [1 2 3 4];
b = ones(2,2,4);

c(:,:,1) = b(:,:,1).*a(1);
c(:,:,2) = b(:,:,2).*a(2);
c(:,:,3) = b(:,:,3).*a(3);
c(:,:,4) = b(:,:,4).*a(4);

Is there any one-line code to do this? Or do I have to think
of some repmat code? Shouldn't there be a way to do this in
a really simple way?

Thanks for your thoughts.

Subject: SIMPLE way to multiply matrix array with vector?

From: Matt Fig

Date: 5 Jun, 2008 13:45:04

Message: 2 of 5

I'm sure there is a much better solution, but this does what
you request:


a = [1 2 3 4];
b = ones(2,2,4);
bsxfun(@times,b,reshape(a,1,1,4))

Subject: SIMPLE way to multiply matrix array with vector?

From: Volker Klink

Date: 5 Jun, 2008 13:55:04

Message: 3 of 5

Yeah, that's what I mean... we all probably know SOME way to
do it. I did it with a reshape and repmat... But in my
opinion it's too intricate (even though it's just 2 or 3
lines of code) fur such a simple operation.

I think they should put some very basic functionality in
MatLab which does that....

Subject: SIMPLE way to multiply matrix array with vector?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 5 Jun, 2008 20:43:33

Message: 4 of 5

In article <g28ql0$3fs$1@fred.mathworks.com>,
Matt Fig <spamanon@yahoo.com> wrote:
>I'm sure there is a much better solution, but this does what
>you request:

>a = [1 2 3 4];
>b = ones(2,2,4);
>bsxfun(@times,b,reshape(a,1,1,4))

Along the same lines,

a = [1 2 3 4];
b = ones(2,2,4);
bsxfun(@times,b,permute(a,[1 3 2]))

--
  "To the modern spirt nothing is, or can be rightly known,
  except relatively and under conditions." -- Walter Pater

Subject: SIMPLE way to multiply matrix array with vector?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 5 Jun, 2008 21:09:02

Message: 5 of 5

In article <g28r7o$bjo$1@fred.mathworks.com>,
Volker Klink <klinkv.NOSPAM@yahoo.de> wrote:
>Yeah, that's what I mean... we all probably know SOME way to
>do it. I did it with a reshape and repmat... But in my
>opinion it's too intricate (even though it's just 2 or 3
>lines of code) fur such a simple operation.

>I think they should put some very basic functionality in
>MatLab which does that....

I think there are much higher priorities considering the ease of
solving the problem by reshape() or permute() to get the dimensions
in the right order.

An example of something I would consider much more fundamental
and significantly less simple to solve by oneself:

Multidimensional matrix multiplication. The * operator only works
on two-dimensional matrices. :(

If you've ever had experience with APL, you know that there are
a lot of algorithms that would normally involve loops but which
can be reformulated in terms of basic operations on higher-dimensional
vectors. Just a +.x here and there...
--
  "The slogans of an inadequate criticism peddle ideas to fashion"
                                              -- Walter Benjamin

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
matrix Volker Klink 5 Jun, 2008 08:05:08
vector Volker Klink 5 Jun, 2008 08:05:08
multiply Volker Klink 5 Jun, 2008 08:05:08
rssFeed for this Thread

Contact us at files@mathworks.com