bugGNU Octave - Bugs: bug #49759, Inconsistent concatenation with...

 
 

bug #49759: Inconsistent concatenation with cat, horzcat, vertcat, and []

Submitter:  Amro <amro_octave>
Submitted:  Sat 03 Dec 2016 12:53:35 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Fri 16 Dec 2016 01:17:01 AM UTC, comment #7: 

I changed do_cat to strip out empty matrices when doing cell concatenation (http://hg.savannah.gnu.org/hgweb/octave/rev/0ab9eecbb165).  I also added the test case in this bug report as a BIST test.  Fixed, closing report.

Rik <rik5>
Group administrator
Tue 13 Dec 2016 05:50:26 AM UTC, comment #6: 

There are two code paths, never a good idea, for concatenation.  One is through the parser and the other is through cat() in data.cc.  The actual function is do_cat which gets called by cat, vertcat, and horzcat.

Rik <rik5>
Group administrator
Wed 07 Dec 2016 03:36:23 PM UTC, comment #5: 

@mtmiller
The type of the empty numeric matrix doesn't seem to matter.
Using "A = int32([])" or "A = single([])" in the previous MATLAB example gives the same results as before.

Amro <amro_octave>
Tue 06 Dec 2016 05:48:43 PM UTC, comment #4: 

I agree, I don't see any open reports about the *cat functions specifically.

The problem looks like it occurs when empty numeric arrays are converted into cell arrays for concatenation. Any homogeneous set collapses correctly:


>> vertcat ({}, {}, {}, {}, {}, {})
ans = {}(0x0)
>> vertcat ([], [], [], [], [], [])
ans = [](0x0)


But as soon as a mixed set is introduced, it adds empty elements to a non-empty cell array.


>> vertcat ({}, {}, {}, {}, {}, [])
ans =
{
  [1,1] = [](0x0)
}


Amro - in Matlab, does the empty array have to be a double or can it be any empty numeric? What does your original example do with "A = int32([])" or "A = rand(0, 'single')"?

Mike Miller <mtmiller>
Group Member
Tue 06 Dec 2016 02:03:05 PM UTC, comment #3: 

Thank you for finding those previous reports.

It's actually interesting because none of the examples in those threads apply anymore in the latest Octave 4.2.0, as the behavior of concatenating using square-brackets is now consistent with that of MATLAB.

I don't know if this was fixed on purpose or simply a happy accident! Either way the issue that remains here is that cat/horzcat/vertcat in Octave are giving different results than the equivalent []-concatenation.

Regardless of whether this concat behavior is considered an undocumented MATLAB feature/bug or not, Octave should at least be consistent with itself no matter how concatenation is performed. So I think that the functions cat/horzcat/vertcat should be fixed to be conformant with [].

Amro <amro_octave>
Mon 05 Dec 2016 05:17:57 PM UTC, comment #2: 

Something similar was reported in bug #37086 and bug #35481.

Guillaume <gyom>
Mon 05 Dec 2016 04:46:45 PM UTC, comment #1: 

Isn't there already a bug report for this?  Could you check to see whether this is a duplicate?

Rik <rik5>
Group administrator
Sat 03 Dec 2016 12:53:35 PM UTC, original submission:  

Concatenating an empty matrix 0x0 with a cell array produce the wrong result, and is inconsistent depending on how we do it.

For example, in Octave 4.2.0 I get this:


>> A = [];
>> B = {1; 2};
>> C = [A; B]
C =
{
  [1,1] =  1
  [2,1] =  2
}
>> C = cat(1, A, B)
C =
{
  [1,1] = [](0x0)
  [2,1] =  1
  [3,1] =  2
}
>> C = vertcat(A, B)
C =
{
  [1,1] = [](0x0)
  [2,1] =  1
  [3,1] =  2
}
>> C = [A B]
C =
{
  [1,1] =  1
  [2,1] =  2
}
>> C = cat(2, A, B)
error: cat: dimension mismatch
>> C = horzcat(A, B)
error: horzcat: cat: dimension mismatch


On the other hand, in MATLAB R2016b I get this:


>> A = [];
>> B = {1; 2};
>> C = [A; B]
C =
  2x1 cell array
    [1]
    [2]
>> C = cat(1, A ,B)
C =
  2x1 cell array
    [1]
    [2]
>> C = vertcat(A, B)
C =
  2x1 cell array
    [1]
    [2]
>> C = [A B]
C =
  2x1 cell array
    [1]
    [2]
>> C = cat(2, A, B)
C =
  2x1 cell array
    [1]
    [2]
>> C = horzcat(A, B)
C =
  2x1 cell array
    [1]
    [2]


So it seems that in Octave using square-brackets to concatenate gives different results than using the equivalent cat/horzcat/vertcat.

Note that if you replace the empty array with an empty cell array (i.e A={} instead of A=[]) then Octave behaves correctly and give the same results as MATLAB in all cases.

Amro <amro_octave>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by gyom (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by amro_octave (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-12-16 rik5 Item GroupIncorrect Result Matlab Compatibility
        StatusNeed Info Fixed
        Open/ClosedOpen Closed
        Release4.2.0 dev
    2016-12-05 rik5 StatusNone Need Info

    Back to the top

    Powered by Savane 3.13-d3ae.
    Corresponding source code