octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fix null assignment


From: Jaroslav Hajek
Subject: Re: fix null assignment
Date: Thu, 18 Sep 2008 16:38:37 +0200

On Thu, Sep 18, 2008 at 3:56 PM, Levente Torok <address@hidden> wrote:
> HI Jaroslav,
>
> Look the example below of Matlab:
>
> a=[1:5]; a(1:5)=[]
>
> a =
>
>  Empty matrix: 1-by-0
>
> but
>
>  a=[1:5]; a(:)=[]
>
> a=
>     []
>
> Why should a(1:length(a)) be different from a(:) in any circumstances?
> I don't think this is a consequent think.

That's a matter of consistency. When you do a(1:n) = [] and you don't
know n, you expect the result to behave consistently. That is, it
should always remain a row vector. Similarly a column vector. a(:) is
something different - it cannot occur as a special case. That's
probably why Matlab defines the result to be always [].


>
> Or.
> a' * a
> ans =
>   0
>
> a * a'
>   []
>
> I think this is very much arbitrary think.

Not at all. If a is m-by-n, I expect a*a' to be m-by-m and a'*a to be
n-by-n, and even the empty matrices should honor this.

>
> I checked documentation of Matlab and I found no notion of empty vectors:
> http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/sum.html
>

Matlab actually has no vectors; it has only matrices. But the
behaviour of empty sum being zero is standard across all languages I
know.

> On the otherhand linear algebra does not deal of such a notion either.
> I feel it to be a bit similar case to operating (0/0) or with inf values.
>
> The most annoying thing is this: Matlab does not have the notion of 
> orientation of vector (horizontal or vertical)

What? Certainly it has. Matlab has no proper vectors, it has only
matrices. Row and column vectors are special cases of a matrix. I'd be
all for it to allow real 1-d arrays in Matlab and make the behaviour
more consistent (e.g., as in Fortran) but that would break *a lot* of
code.

> Then why would an empty vector have such a property?
>
> I think it was made to ease the work of program writers by generating error in
> cases where no data is get yet but the vectors are not oriented properly in 
> an inner product or an outer product
> as such:
>
> a =
>
>   Empty matrix: 1-by-0
>
>>> a * a
> ??? Error using ==> mtimes
> Inner matrix dimensions must agree.
>
> I believe this can be hand but results in exceptional cases to be handled in 
> most of the cases.

I don't think so. In fact, I think that it results in less exceptional cases.


> Let me show another:
>
>>> isfloat( [] )
> ans =
>        1
>
> why is this?
>

isfloat is true on all doubles, probably to ease mixing singles and doubles.

> Levente
>
> On Thursday 18 September 2008, Jaroslav Hajek wrote:
>> On Thu, Sep 18, 2008 at 2:23 PM, Levente Torok <address@hidden> wrote:
>> > On Thursday 18 September 2008, Jaroslav Hajek wrote:
>> >
>> > Hi all,
>> >
>> > However, I think matlab compatibility is the top priority
>> > I believe octave was much more consistent in terms of mat
>> >
>> >> hello,
>> >>
>> >> the attached patch fixes the following problems / Matlab
>> >> incompatibilities with null assignment in Octave.
>> >>
>> >> 2. a = 1:5; a(1:5) = []
>> >> --> a is 0-by-0 in Octave, 1-by-0 in Matlab
>> >
>> >> 3. a = 1:5; a = a'; a(1:5) = []
>> >> --> a is 0-by-0 in Octave, 0-by-1 in Matlab
>> >
>> >
>> >> 4. a = ones (3); a(:,:) = []
>> >> --> a is 0-by-0 in Octave, 0-by-3 in Matlab
>> >
>> >> 5. a = ones (3); a(1:3,1:3) = []
>> >> --> a is 0-by-0 in Octave, error in Matlab
>> >
>> >> 6. a = ones (3); a(1:2,1:2) = []
>> >> --> a is unaffected in Octave (! BUG), error in Matlab
>> >
>> > I don't believe it makes much sense to follow Matlab in these aspects
>> > with the exception of the bug.
>> >
>> > To make myself clear why I think so:
>> >
>> > a = 1:5; a(1:5) = []; a * a'
>> > is 0 in matlab and ans = [](0x0)
>> >
>> > While the matlab version is handy when dealing with real data but faulty 
>> > in terms of math.
>> > I mean how can a scalar product equal to 0 when there are no items in the 
>> > product.
>> >
>>
>
>> An empty sum is *defined* to be zero,
>> and a matrix product with the
>> *inner* dimension empty is nothing else than an empty sum.
>> Therefore, this behaviour is completely OK. I'm glad that it also
>> works with the patch I've posted.
>
>
>> Similarly, an empty product is defined as unity.
> ??
> I guess you meant zero instead of unity.

Not at all. See http://en.wikipedia.org/wiki/Empty_product

>
> Lev
>
> --
> Blogger of http://fapuma.blogspot.com
>



-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]