Thread Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Giovanni Gherdovich

Date: 12 Jun, 2008 22:55:56

Message: 1 of 11

Hello,

sorry for the subject of this post, but I was really surprised
by the behaviour of my Matlab 7 R14.

At the origin of this post there is my naive assumption that

1 + 2:4

should evaluate to

[3 4 5]

but this is not (always) the case.

Here is a Matlab session:

>> 1 + 2:4
ans =
     3 4

>> a = 2:4;
>> 1 + a
ans =
     3 4 5

Why (1 + 2:3) = [3 4] ?
And why the second time I got a different result?

Now I tried to invent some funny jokes,
(I must say without success -- I'm sure you can do better):

>> zero = 0:0
zero =
     0
>> 1 + zero
ans =
     1

>> 1 + 0:0
ans =
   Empty matrix: 1-by-0

>> (1 + zero) == (1 + 0:0)
ans =
   Empty matrix: 1-by-0

>> islogical((1 + zero) == (1 + 0:0))
ans =
     1

>> zero - 0:0
ans =
     0

>> 1 + zero - 0:0
ans =
   Empty matrix: 1-by-0

>> 1 + (zero - 0:0)
ans =
     1

Apart the jokes, I would really like to know more
about the semantics of the Matlab Programming Language.

Cheers,
GGhh


Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Scott Burnside

Date: 12 Jun, 2008 23:06:02

Message: 2 of 11


> >> 1 + 2:4
> ans =
> 3 4
>
> >> a = 2:4;
> >> 1 + a
> ans =
> 3 4 5

This is is explained by operator precedence.

In the first case 1 is being added to 2 and the result (3)
is being indexed to 4 so you end up with 3 4.

In the second case you have forced the index operation to
occur before the addition and so have gotten a different
result.

hth,
Scott

Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Giovanni Gherdovich

Date: 12 Jun, 2008 23:14:54

Message: 3 of 11

> This is is explained by operator precedence.

Aargh!

>> 1 + (2:4)

ans =

     3 4 5

You're right, thankyou.

GGhh

Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Loren Shure

Date: 13 Jun, 2008 12:03:08

Message: 4 of 11

In article <07767cbe-e1ff-4180-9877-
c78cdcbe532c@d77g2000hsb.googlegroups.com>,
gherdovich@students.math.unifi.it says...
> Hello,
>
> sorry for the subject of this post, but I was really surprised
> by the behaviour of my Matlab 7 R14.
>
> At the origin of this post there is my naive assumption that
>
> 1 + 2:4
>
> should evaluate to
>
> [3 4 5]
>
> but this is not (always) the case.
>
> Here is a Matlab session:
>
> >> 1 + 2:4
> ans =
> 3 4
>
> >> a = 2:4;
> >> 1 + a
> ans =
> 3 4 5
>
> Why (1 + 2:3) = [3 4] ?
> And why the second time I got a different result?
>
> Now I tried to invent some funny jokes,
> (I must say without success -- I'm sure you can do better):
>
> >> zero = 0:0
> zero =
> 0
> >> 1 + zero
> ans =
> 1
>
> >> 1 + 0:0
> ans =
> Empty matrix: 1-by-0
>

1+0 = 1
1:0 is empty

> >> (1 + zero) == (1 + 0:0)
> ans =
> Empty matrix: 1-by-0
>
> >> islogical((1 + zero) == (1 + 0:0))
> ans =
> 1
>
> >> zero - 0:0
> ans =
> 0
>
> >> 1 + zero - 0:0
> ans =
> Empty matrix: 1-by-0
>
> >> 1 + (zero - 0:0)
> ans =
> 1
>
> Apart the jokes, I would really like to know more
> about the semantics of the Matlab Programming Language.
>
> Cheers,
> GGhh
>
>
>

--
Loren
http://blogs.mathworks.com/loren/

Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Jos

Date: 13 Jun, 2008 14:44:03

Message: 5 of 11

Can somebody explain the following warning inconsistencies
(in R14)?


  c = [1 ; zeros(0,1)]
  % c = 1
  c = [1 ; zeros(0,2)]
Warning: Concatenation involves an empty array with
incorrect number of columns.
??? Error using ==> vertcat
Dimensions 2 thru maximum dimension must match in []
concatenation for each component.

Ok, I can understand that, but:

  c = [1 ; zeros(0,0)]
  c = [1 ; zeros(1,0)]

are both ok, but then again:

  c = [1 ; zeros(2,0)]
Warning: Concatenation involves an empty array with
incorrect number of columns.
??? Error using ==> vertcat
Dimensions 2 thru maximum dimension must match in []
concatenation for each component.

I cannot see the "incorrect number of columns" argument here.

Jos


Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Steven Lord

Date: 14 Jun, 2008 03:08:41

Message: 6 of 11


"Giovanni Gherdovich" <gherdovich@students.math.unifi.it> wrote in message
news:07767cbe-e1ff-4180-9877-c78cdcbe532c@d77g2000hsb.googlegroups.com...
> Hello,
>
> sorry for the subject of this post, but I was really surprised
> by the behaviour of my Matlab 7 R14.
>
> At the origin of this post there is my naive assumption that
>
> 1 + 2:4
>
> should evaluate to
>
> [3 4 5]
>
> but this is not (always) the case.

As others have stated, this is due to operator precedence. To see the list
of which operators take precedence over which other operators, type "help
precedence" without the quotes at the prompt.

You'll see that addition is at level 4 on the precedence hierarchy (lower
number == performed first) and colon is at level 5.

> Here is a Matlab session:
>
>>> 1 + 2:4
> ans =
> 3 4

+ before :

>>> a = 2:4;
>>> 1 + a
> ans =
> 3 4 5

In this case, : is done first (when you assign the result of that expression
to a) and then + is performed on the next line.

> Why (1 + 2:3) = [3 4] ?
> And why the second time I got a different result?
>
> Now I tried to invent some funny jokes,
> (I must say without success -- I'm sure you can do better):
>
>>> zero = 0:0
> zero =
> 0
>>> 1 + zero
> ans =
> 1
>
>>> 1 + 0:0
> ans =
> Empty matrix: 1-by-0
>
>>> (1 + zero) == (1 + 0:0)
> ans =
> Empty matrix: 1-by-0
>
>>> islogical((1 + zero) == (1 + 0:0))
> ans =
> 1

The result of == should always (unless someone has specifically overloaded
it for a class) be a logical array.

*snip*

--
Steve Lord
slord@mathworks.com


Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Loren Shure

Date: 16 Jun, 2008 16:34:06

Message: 7 of 11

In article <g2u13j$6vv$1@ginger.mathworks.com>, DELjos@jasenDEL.nl
says...
> Can somebody explain the following warning inconsistencies
> (in R14)?
>
>
> c = [1 ; zeros(0,1)]
> % c = 1
> c = [1 ; zeros(0,2)]
> Warning: Concatenation involves an empty array with
> incorrect number of columns.
> ??? Error using ==> vertcat
> Dimensions 2 thru maximum dimension must match in []
> concatenation for each component.
>
> Ok, I can understand that, but:
>
> c = [1 ; zeros(0,0)]
> c = [1 ; zeros(1,0)]
>
> are both ok, but then again:
>
> c = [1 ; zeros(2,0)]
> Warning: Concatenation involves an empty array with
> incorrect number of columns.
> ??? Error using ==> vertcat
> Dimensions 2 thru maximum dimension must match in []
> concatenation for each component.
>
> I cannot see the "incorrect number of columns" argument here.
>
> Jos
>

But it is incorrect. You are trying to build a column vector (since
there's the ; in the statement). The first entry, 1, has one column.
the second entry, zeros(2,0) has 0 columns.

--
Loren
http://blogs.mathworks.com/loren/

Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Jos

Date: 16 Jun, 2008 18:19:02

Message: 8 of 11

Loren Shure <loren@mathworks.com> wrote in message
<MPG.22c06009b804cff4989866@news.mathworks.com>...
> In article <g2u13j$6vv$1@ginger.mathworks.com>,
DELjos@jasenDEL.nl
> says...
> > Can somebody explain the following warning
inconsistencies
> > (in R14)?
> >
> >
> > c = [1 ; zeros(0,1)]
> > % c = 1
> > c = [1 ; zeros(0,2)]
> > Warning: Concatenation involves an empty array with
> > incorrect number of columns.
> > ??? Error using ==> vertcat
> > Dimensions 2 thru maximum dimension must match in []
> > concatenation for each component.
> >
> > Ok, I can understand that, but:
> >
> > c = [1 ; zeros(0,0)]
> > c = [1 ; zeros(1,0)]
> >
> > are both ok, but then again:
> >
> > c = [1 ; zeros(2,0)]
> > Warning: Concatenation involves an empty array with
> > incorrect number of columns.
> > ??? Error using ==> vertcat
> > Dimensions 2 thru maximum dimension must match in []
> > concatenation for each component.
> >
> > I cannot see the "incorrect number of columns" argument
here.
> >
> > Jos
> >
>
> But it is incorrect. You are trying to build a column
vector (since
> there's the ; in the statement). The first entry, 1, has
one column.
> the second entry, zeros(2,0) has 0 columns.
>
> --
> Loren
> http://blogs.mathworks.com/loren/


I think I should have explained myself better.
I can understand the error of vertcat, but not the
preceding warning: "Concatenation involves an empty array
with incorrect number of columns."

Why does [1 ; zeros(2,0)] issue a warning, but not [1 ;
zeros(1,0)] , both having zero columns?

Jos

Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Loren Shure

Date: 16 Jun, 2008 20:50:44

Message: 9 of 11

In article <g36aqm$q7l$1@fred.mathworks.com>, DELjos@jasenDEL.nl says...
> Loren Shure <loren@mathworks.com> wrote in message
> <MPG.22c06009b804cff4989866@news.mathworks.com>...
> > In article <g2u13j$6vv$1@ginger.mathworks.com>,
> DELjos@jasenDEL.nl
> > says...
> > > Can somebody explain the following warning
> inconsistencies
> > > (in R14)?
> > >
> > >
> > > c = [1 ; zeros(0,1)]
> > > % c = 1
> > > c = [1 ; zeros(0,2)]
> > > Warning: Concatenation involves an empty array with
> > > incorrect number of columns.
> > > ??? Error using ==> vertcat
> > > Dimensions 2 thru maximum dimension must match in []
> > > concatenation for each component.
> > >
> > > Ok, I can understand that, but:
> > >
> > > c = [1 ; zeros(0,0)]
> > > c = [1 ; zeros(1,0)]
> > >
> > > are both ok, but then again:
> > >
> > > c = [1 ; zeros(2,0)]
> > > Warning: Concatenation involves an empty array with
> > > incorrect number of columns.
> > > ??? Error using ==> vertcat
> > > Dimensions 2 thru maximum dimension must match in []
> > > concatenation for each component.
> > >
> > > I cannot see the "incorrect number of columns" argument
> here.
> > >
> > > Jos
> > >
> >
> > But it is incorrect. You are trying to build a column
> vector (since
> > there's the ; in the statement). The first entry, 1, has
> one column.
> > the second entry, zeros(2,0) has 0 columns.
> >
> > --
> > Loren
> > http://blogs.mathworks.com/loren/
>
>
> I think I should have explained myself better.
> I can understand the error of vertcat, but not the
> preceding warning: "Concatenation involves an empty array
> with incorrect number of columns."
>
> Why does [1 ; zeros(2,0)] issue a warning, but not [1 ;
> zeros(1,0)] , both having zero columns?
>
> Jos
>

In the second case, at least one of the dimensions agrees - the rows in
that case. MATLAB tries to be lenient and allow such expressions to
occur error-free.

--
Loren
http://blogs.mathworks.com/loren/

Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Matt Fig

Date: 16 Jun, 2008 21:01:04

Message: 10 of 11


>
> In the second case, at least one of the dimensions agrees
- the rows in
> that case. MATLAB tries to be lenient and allow such
expressions to
> occur error-free.


Sometimes....

[[1;1] ; zeros(2,0)]

Subject: The Empty Matrix is a logical vector, 1 = 0 and other funny bugs

From: Loren Shure

Date: 17 Jun, 2008 12:12:30

Message: 11 of 11

In article <g36kag$aoj$1@fred.mathworks.com>, spamanon@yahoo.com says...
>
> >
> > In the second case, at least one of the dimensions agrees
> - the rows in
> > that case. MATLAB tries to be lenient and allow such
> expressions to
> > occur error-free.
>
>
> Sometimes....
>
> [[1;1] ; zeros(2,0)]
>
>

Same reasoning - you have a 2x1 and are attempting to vertically
concatenate a 2x0 - not same number of columns. Perhaps you meant:

[[1;1] , zeros(2,0)]

Note , vs. ; - this gives a 2x1 vector of ones with no warning message
in R2008a.


--
Loren
http://blogs.mathworks.com/loren/

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
operator precedence a programmer 12 Jun, 2008 19:10:22
rssFeed for this Thread

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.

Contact us at files@mathworks.com