Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: repmat, reshape, bsxfun
Replies: 12   Last Post: Jan 23, 2012 11:08 AM

 Messages: [ Previous | Next ]
 Pag Max Posts: 13 Registered: 5/16/11
repmat, reshape, bsxfun
Posted: Jan 20, 2012 1:00 PM

Is there a smarter or shorter way to do this than what I have done? (Of course withot loop!!). I am thinking there is something repetitive in my solution
c is tiled in third dimension as many times as elements in b and then each tile is scaled by corresponding element in b.

c=[1 2 3;4 5 6];
b=[1 2 3 4 5];
a=bsxfun(@times,repmat(c,[1 1 numel(b)]),reshape(b,[1 1 numel(b)]));

gives
a=
a(:,:,1) =

1 2 3
4 5 6

a(:,:,2) =

2 4 6
8 10 12

a(:,:,3) =

3 6 9
12 15 18

a(:,:,4) =

4 8 12
16 20 24

a(:,:,5) =
5 10 15
20 25 30

Date Subject Author
1/20/12 Pag Max
1/22/12 Matt J
1/22/12 Bruno Luong
1/22/12 Matt J
1/22/12 James Tursa
1/22/12 james bejon
1/22/12 James Tursa
1/23/12 Pag Max
1/23/12 Bruno Luong
1/23/12 Pag Max
1/23/12 Bruno Luong
1/23/12 James Tursa
1/22/12 James Tursa