I have a question about how MatLab treats vectors and scalars. It
appears there are instances where MatLab will will implicitly treat a
scalar like a vector. Here is a simple example:
x = [1,2];
y = 1+x
where MatLab returns a value of y=[2,3]. Here MatLab treats the scalar
"1" in the expression for y implicitly as a vector.
I am wondering why MatLab does this. The motivation for my question is
that I personally like this feature (even though it is not good
programming style), but I'd like to make certain there are not cases/
circumstances where using it would cause me problems.
junoexpress <MTBrenneman@gmail.com> wrote in news:cab4169a-2112-470d-afba-
4c27a5858cca@8g2000hse.googlegroups.com:
> but I'd like to make certain there are not cases/
> circumstances where using it would cause me problems.
>
>
It doesn't seem much different to producing a long result when adding a
short to a long. In the example you cite, adding a vector to a scalar,
what would you prefer, an error?
Matlab will usually treat most operations as if they are operations on
whole arrays, and you will get an error when array sizes don't match.
Scalars will be automatically be expanded to arrays of the appropriate size
for the operation when it makes no sense to perform the operation on a
scalar and an array.
As to whether this causes you problems, it rarely, but not never, causes
issues for me. Without a description of what you do, it would be hard to
say how it will impact you.
"Scott Seidman" <namdiesttocs@mindspring.com> schrieb im Newsbeitrag
news:Xns9AE3944E42AA4scottseidmanmindspri@130.133.1.4...
> junoexpress <MTBrenneman@gmail.com> wrote in news:cab4169a-2112-470d-afba-
> 4c27a5858cca@8g2000hse.googlegroups.com:
>
>> but I'd like to make certain there are not cases/
>> circumstances where using it would cause me problems.
>>
>>
>
> It doesn't seem much different to producing a long result when adding a
> short to a long. In the example you cite, adding a vector to a scalar,
> what would you prefer, an error?
>
> Matlab will usually treat most operations as if they are operations on
> whole arrays, and you will get an error when array sizes don't match.
> Scalars will be automatically be expanded to arrays of the appropriate
> size
> for the operation when it makes no sense to perform the operation on a
> scalar and an array.
>
> As to whether this causes you problems, it rarely, but not never, causes
> issues for me. Without a description of what you do, it would be hard to
> say how it will impact you.
>
> --
> Scott
> Reverse name to reply
Hi,
in addition: open the doc and search for "scalar expansion". The first hit
describes this (and similar topics) in detail.
>
> Hi,
> in addition: open the doc and search for "scalar expansion". The first hit
> describes this (and similar topics) in detail.
>
> Titus
That explains my question exactly, and moreover, addresses my concern
that this is a built in inherent feature of MatLab rather than
something you can get away with due to some loophole in the program.
junoexpress <MTBrenneman@gmail.com> wrote in
news:e9b8c3c4-2d48-4b9f-aa28-b698aa4381c7@m3g2000hsc.googlegroups.com:
>
>>
>> Hi,
>> in addition: open the doc and search for "scalar expansion". The
>> first hit describes this (and similar topics) in detail.
>>
>> Titus
>
> That explains my question exactly, and moreover, addresses my concern
> that this is a built in inherent feature of MatLab rather than
> something you can get away with due to some loophole in the program.
>
> Thank you,
>
> Matt
>
>
There will always be some scalar/vector/array issues that pop up. One bit
me once, and its a fair illustrative example. I have an array where rows
are data for an experiment. I toss out rows that don't meet certain
criteria, and then use "mean". If there is more than one row, mean returns
the mean of each colum, but if there is only one row, mean returns the mean
of that row.
"Scott Seidman" <namdiesttocs@mindspring.com> wrote in message
news:Xns9AE45777FFEC4scottseidmanmindspri@130.133.1.4...
> junoexpress <MTBrenneman@gmail.com> wrote in
> news:e9b8c3c4-2d48-4b9f-aa28-b698aa4381c7@m3g2000hsc.googlegroups.com:
>
>>
>>>
>>> Hi,
>>> in addition: open the doc and search for "scalar expansion". The
>>> first hit describes this (and similar topics) in detail.
>>>
>>> Titus
>>
>> That explains my question exactly, and moreover, addresses my concern
>> that this is a built in inherent feature of MatLab rather than
>> something you can get away with due to some loophole in the program.
>>
>> Thank you,
>>
>> Matt
>>
>>
>
> There will always be some scalar/vector/array issues that pop up. One bit
> me once, and its a fair illustrative example. I have an array where rows
> are data for an experiment. I toss out rows that don't meet certain
> criteria, and then use "mean". If there is more than one row, mean
> returns
> the mean of each colum, but if there is only one row, mean returns the
> mean
> of that row.
That's true. One way to avoid that issue is to explicitly specify the
dimension over which you want the computations performed; many of the data
functions (including MEAN) allow you to do so.
x = rand(1, 10);
m0 = mean(x); % scalar -- the mean of the row
m1 = mean(x, 1); % vector -- the mean of each column
--
Steve Lord
slord@mathworks.com
Tags for this Thread
Add a New Tag:
Separated by commas
Ex.: root locus, bode
What are tags?
A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.
Anyone can tag a thread. Tags are public and visible to everyone.
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.