octave-bug-tracker
[Top][All Lists]
Advanced

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

[Octave-bug-tracker] [bug #56548] Inconsistent behavior between horzcat/


From: anonymous
Subject: [Octave-bug-tracker] [bug #56548] Inconsistent behavior between horzcat/vertcat/cat and bracket syntax
Date: Tue, 25 Jun 2019 16:32:11 -0400 (EDT)
User-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36

URL:
  <https://savannah.gnu.org/bugs/?56548>

                 Summary: Inconsistent behavior between horzcat/vertcat/cat
and bracket syntax
                 Project: GNU Octave
            Submitted by: None
            Submitted on: Tue 25 Jun 2019 08:32:10 PM UTC
                Category: Octave Function
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Unexpected Error
                  Status: None
             Assigned to: None
         Originator Name: Geoffrey K Adams
        Originator Email: address@hidden
             Open/Closed: Open
         Discussion Lock: Any
                 Release: 4.2.2
        Operating System: GNU/Linux

    _______________________________________________________

Details:

When concatenating an empty matrix with a struct, the functions horzcat and
vertcat generate an error, while the bracket concatenation syntax behaves as
expected. Curiously, cat is inconsistent depending on the order of the
arguments.

The expected (Matlab-consistent, and also convenient from a programming
standpoint) behavior is that empty numeric matrices are dropped entirely,
treated effectively as though they were the same type as the struct. This
behavior is seen with bracket concatenation:


>>> [[], struct]
ans =
  scalar structure containing the fields:

>>> [[]; struct]
ans =
  scalar structure containing the fields:

>>> [struct, []]
ans =
  scalar structure containing the fields:


However, when using horzcat or vertcat, the same concatenation unexpectedly
produces an error:


>>> horzcat([], struct)
error: concatenation operator not implemented for 'scalar' by 'scalar struct'
operations

>>> vertcat([], struct)
error: concatenation operator not implemented for 'scalar' by 'scalar struct'
operations

>>> horzcat(struct, [])
error: concatenation operator not implemented for 'scalar struct' by 'matrix'
operations


Perhaps more concerning, the cat function does not produce an error but
instead gives inconsistent results depending on whether the struct or the
empty matrix is the first input:


>>> cat(3, struct, [])
ans =
  scalar structure containing the fields:
>>> cat(3, [], struct)
ans = 0


It seems cat is choosing the type of its output based on the type of the first
input (after the dimension specifier).

Note that the above examples are somewhat trivial, but I sometimes use a
programming pattern like the following:


items = cell(n,1)
for i=1:n
    % calculate a condition
    if condition
        items{i} = struct(...);
    end
end
items = vertcat(items{:});


so that it may not be clear which elements are actually assigned, but I can
rely on the interpreter's error handling to catch if any of the items which
were assigned don't have matching fields.

Workarounds:

horzcat(args{:}) and vertcat(args{:}) can be replaced by [args{:}] and
[args{:}]', respectively. I sometimes prefer to use horzcat and vertcat
because the transpose operator is easy to miss when reading code and the
function forms make it very explicit which type of concatenation is being
performed. However, cat(D, args{:}) is less straightforward to workaround. and
would require explicitly testing with isempty (or something similar) to remove
unassigned elements.

(c.f. bug #36817)




    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?56548>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

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