help-octave
[Top][All Lists]

## Re: Meaning of * (mtimes) for nd-arrays

 From: Nicholas Jankowski Subject: Re: Meaning of * (mtimes) for nd-arrays Date: Tue, 16 Feb 2016 12:55:17 -0500

On Mon, Feb 15, 2016 at 8:01 AM, Juan Pablo Carbajal wrote:

On Mon, Feb 15, 2016 at 10:53 AM, Marco Atzeri wrote:
On 15/02/2016 10:41, Marco Caliari wrote:
Dear all,

I recently discovered that [1;2;3;4]*ones(1,1,3), for instance, gives a
four-by-three matrix. I would have expected an error (like in Matlab) or
a four-by-one-by-three array. So, what is the definition? Is it just
squeeze("outer product")? Is it documented? A similar thing happens with
kron.

Thanks,

Marco

_______________________________________________
Help-octave mailing list
https://lists.gnu.org/mailman/listinfo/help-octave

[1;2;3;4] * ones(1,1,3) is the same as [1;2;3;4] * ones(1,3) and is not boradcasting. It is simply applying the rule of multiplication in matrices by ignoring the leading singletons. I think matlab would do the same, can you verify?

Boradcasting would be produce the same as

bsxfun(@mtimes,[1;2;3;4],ones(1,1,3))

which is shortened in Octave (and not in matlab) with

[1;2;3;4] .* ones(1,1,3)

was playing with nD arrays a bit last summer and this caught my eye.

ones(1,1,3) is not the same as ones(1,3).  it produces a 1x1x3 array, whereas the latter produces a 1x3 array  (ones does not apply a squeeze or anything to force 2D)

in Matlab:

>> [1;2;3;4] * ones(1,1,3)
Error using *
Inputs must be 2-D, or at least one input must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead.

>> [1;2;3;4] * ones(1,3)
ans =
1 1 1
2 2 2
3 3 3
4 4 4

>> bsxfun(@mtimes,[1;2;3;4],ones(1,1,3))

ans(:,:,1) =
1
2
3
4

ans(:,:,2) =
1
2
3
4

ans(:,:,3) =
1
2
3
4

(which is the same output in octave using .* as you said)

ALL THAT SAID...

In Octave:

>> [1;2;3;4]*ones(1,1,3)

ans =

1   1   1
2   2   2
3   3   3
4   4   4

so there's something off here.

Nick J