[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Broadcasting equivalent for multiplication?
From: |
Jordi Gutiérrez Hermoso |
Subject: |
Re: Broadcasting equivalent for multiplication? |
Date: |
Wed, 1 May 2013 15:57:50 -0400 |
On 1 May 2013 15:37, Nir Krakauer <address@hidden> wrote:
> I have an n*m array X and an n*k array Y. Is there a shortcut to get
> an n*m*k array Z such that
>
> Z(:, i, j) is equal to X(:, i) .* Y(:, j) ?
So you want a 3d multiplication table? Does this work?
X = rand (3, 5); Y = rand (3, 7);
Z = X.*permute (Y, [1,3,2]);
## Check results
for i = 1:size (Z)(2)
for j = 1:size (Z)(3)
isequal (Z(:, i, j), X(:, i).*Y(:, j))
endfor
endfor
The trick is to see how to put the dimensions in the right location, I
think. Look at how you want [n, m, k] resulting dimensions. X has
dimensions [n, m, 1], so it's already correct, and Y has
dimensions [n, k, 1], so the permutation [1, 3, 2] swaps the last two
dimensions as needed.
HTH,
- Jordi G. H.