0

Suppose I have an array M of dimension M = (3, 12, 9) and a vector C with 9 elements.

How do I efficiently multiply M with C so that

M(:,:,1) * C(1)
M(:,:,2) * C(2)

and so forth?

help is much appreciated. Thanks in advance

CD86
  • 979
  • 10
  • 27

1 Answers1

1

Code:

bsxfun(@times,A,permute(C(:),[3 2 1]));

orginally answered by Divakar taken from here

Community
  • 1
  • 1
Santhan Salai
  • 3,888
  • 19
  • 29
  • I want to scale each individual 3,12 submatrix with the corresponding value in C. @Santhan, I dont get how this is suppose to help... can you elaborate? – CD86 Apr 06 '15 at 12:25
  • Edited. See the link mentioned. – Santhan Salai Apr 06 '15 at 12:27
  • 2
    Would be nice to mention the assumption that vector C is a row vector, or do `bsxfun(@times,A,permute(C(:).',[3 1 2]));` or `bsxfun(@times,A,permute(C(:),[3 2 1]));` without stating any assumption. – Divakar Apr 06 '15 at 12:48
  • thanks a lot but here is a follow up question: Suppose I have a matrix M = (3, 12, 4) and I want to transform it so that M = (12,12) basically just concatenating the individual (3,12) submatrices under each other. How do I do that? Thanks in advance – CD86 Apr 06 '15 at 12:52
  • 1
    @CarstenD That "concatenating under each other" operation is also dealt with in that [other answer](http://stackoverflow.com/a/23808285/3293881) at the end of it. – Divakar Apr 06 '15 at 12:55
  • @CarstenD see [reshape](http://in.mathworks.com/help/matlab/ref/reshape.html?refresh=true) – Santhan Salai Apr 06 '15 at 12:57
  • @Divakar, I'm a begginer and didn't knew `anyVector(:)` will always return a column vector. Its great to know. Thanks once again for pointing and correcting :) – Santhan Salai Apr 06 '15 at 13:18