From: Niles Martinsen on
Hi

1) .* is an array operator (element-by-element operator). Now, if a=2 and x=[1 2], then why am I allowed to calculate a.*x? I mean, it is an element-by-element operator, so it should give me an error.

2) Furthermore, * is a matrix-operator (according to Support - Code Vectorization Guide). If that is the case, then why am I allowed to calculate the product x*a? Only a*x should be allowed.

I hope you will help me by shedding some light on this.

Best,
Niles.
From: us on
"Niles Martinsen" <niels.martinsen(a)gmail.com> wrote in message <i1k6sd$m4q$1(a)fred.mathworks.com>...
> Hi
>
> 1) .* is an array operator (element-by-element operator). Now, if a=2 and x=[1 2], then why am I allowed to calculate a.*x? I mean, it is an element-by-element operator, so it should give me an error.
>
> 2) Furthermore, * is a matrix-operator (according to Support - Code Vectorization Guide). If that is the case, then why am I allowed to calculate the product x*a? Only a*x should be allowed.
>
> I hope you will help me by shedding some light on this.
>
> Best,
> Niles.

well...
it might sound simple (but it IS in the docs...)...
- a SCALAR is always treated differently by ML and overrides any MxN rule...

us
From: Bruno Luong on
"Niles Martinsen" <niels.martinsen(a)gmail.com> wrote in message <i1k6sd$m4q$1(a)fred.mathworks.com>...
> Hi
>
> 1) .* is an array operator (element-by-element operator). Now, if a=2 and x=[1 2], then why am I allowed to calculate a.*x? I mean, it is an element-by-element operator, so it should give me an error.

Matlab does automatic the so called "scalar-expansion" whenever it's possible so as arithmetic matrix with scalar is valid.
>
> 2) Furthermore, * is a matrix-operator (according to Support - Code Vectorization Guide). If that is the case, then why am I allowed to calculate the product x*a? Only a*x should be allowed.
>

Again this is Matlab convenient automatic expansion. The purpose is correctly represent linear operator:

X*a is a matrix such that
X*(a*v) = (X*a)*v, for any 'v' vector and 'a' scalar matrix

Bruno
From: Steven Lord on

"Niles Martinsen" <niels.martinsen(a)gmail.com> wrote in message
news:i1k6sd$m4q$1(a)fred.mathworks.com...
> Hi
>
> 1) .* is an array operator (element-by-element operator). Now, if a=2 and
> x=[1 2], then why am I allowed to calculate a.*x? I mean, it is an
> element-by-element operator, so it should give me an error.

Convenience. Technically, if we stuck to the strict definition of + as an
elementwise operator and REQUIRED the inputs to be the same size, then to
add 1 to this matrix:

x = rand(1000, 2000);

we would need to either loop over all 2 million elements of x, or create a
1000-by-2000 matrix where each element contains the value 1 to perform that
addition. That's not the most efficient (in terms of time for the former or
memory for the latter) approach. Instead, if you're using an elementwise
operator, and one of the inputs is scalar, it's treated "as though" it was
the same size as the other input. Now even though the term for this
technique is scalar _expansion_, the scalar is not always expanded --
sometimes it's contracted.

x = zeros(0, 1);
y = x+5

In this case, the 1-by-1 (scalar) value 5 is "expanded" as though it were
0-by-1 and added to x and the resulting y should be the 0-by-1 empty matrix.

> 2) Furthermore, * is a matrix-operator (according to Support - Code
> Vectorization Guide). If that is the case, then why am I allowed to
> calculate the product x*a? Only a*x should be allowed.

It's a similar argument. When you say x*a, with a a scalar, you're trying
to scale each element of x. To do this without using a scalar
expansion-like approach you could loop over the elements of x and scale each
one individually, or you could create a (potentially large) scaled identity
(which would itself require some manipulation to create) and perform
matrix-matrix multiplication. Or you could treat this as a special case and
just scale each element.

> I hope you will help me by shedding some light on this.

For more information, you can read Loren's blog posting about scalar
expansion:

http://blogs.mathworks.com/loren/2006/02/22/scalar-expansion-and-more-take-2/

or Doug's video blog about the topic:

http://blogs.mathworks.com/videos/2007/11/29/matlab-basics-scalar-and-vector-expansion/

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com