Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Mathematics
General TopicsResearchOperations ResearchStatisticsMathematical LogicNumerical AnalysisUndergraduate MathAlgebra HelpRecreational Math
Math Software
MapleMathematicaMATLABScilabSASSPSS

Math Forum / Math Software / MATLAB / July 2008



Tip: Looking for answers? Try searching our database.

How MatLab Treats Scalars and Vectors

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
junoexpress - 22 Jul 2008 19:19 GMT
Hi,

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.

TIA,

Matt
Scott Seidman - 22 Jul 2008 19:34 GMT
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.

Signature

Scott
Reverse name to reply

Titus - 22 Jul 2008 20:27 GMT
> junoexpress <MTBrenneman@gmail.com> wrote in news:cab4169a-2112-470d-afba-
> 4c27a5858cca@8g2000hse.googlegroups.com:
[quoted text clipped - 16 lines]
> issues for me.  Without a description of what you do, it would be hard to
> say how it will impact you.

Hi,
in addition: open the doc and search for "scalar expansion". The first hit
describes this (and similar topics) in detail.

Titus
junoexpress - 23 Jul 2008 05:40 GMT
> 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
Scott Seidman - 23 Jul 2008 13:35 GMT
>> Hi,
>> in addition: open the doc and search for "scalar expansion". The
[quoted text clipped - 9 lines]
>
> 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.

Signature

Scott
Reverse name to reply

Steven Lord - 23 Jul 2008 15:05 GMT
>>> Hi,
>>> in addition: open the doc and search for "scalar expansion". The
[quoted text clipped - 18 lines]
> 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

Signature

Steve Lord
slord@mathworks.com

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.