bugGNU Octave - Bugs: bug #65228, matlab for loops on empty row...

 
 

bug #65228: matlab for loops on empty row vector

Submitter:  A.R. Burgers <arb>
Submitted:  Tue 30 Jan 2024 08:19:58 AM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Duplicate 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

Tue 30 Jan 2024 07:46:03 PM UTC, comment #6: 

it seems this is the same issue discussed in bug #55622.  Adding the dependency and closing this as a duplicate. if i'm missing an aspect that extends beyond that other one we can reopen it.

Nicholas Jankowski <nrjank>
Group Member
Tue 30 Jan 2024 06:10:07 PM UTC, comment #5: 

This has come up before.  See bug #55622 for more extensive discussion.

There were mixed opinions.  I consider it stupid, confusing, and an artifact of Matlab's implementation rather than a design choice.  But, jwe has pointed out that this is the documented behavior, that Matlab follows its documentation in this case, and that Octave is therefore Matlab-incompatible which may surprise some coders.  I would argue, in turn, that well-written code should be clear as to purpose and therefore it would be better for programmers to recode their for loop rather than use this "feature".  In sum, we didn't make a change in 2017, and declined again in 2020.  Maybe opinions have shifted since then.

Rik <rik5>
Group administrator
Tue 30 Jan 2024 03:27:50 PM UTC, comment #4: 

in matlab 2023b the comment #0 behavior is still the same.


>> nc = 4;
z = zeros(0, nc)
z_empty = isempty(z)
for i = z
  a = i
end
z =
  0×4 empty double matrix
z_empty =
  logical
   1
a =
  0×1 empty double column vector
a =
  0×1 empty double column vector
a =
  0×1 empty double column vector
a =
  0×1 empty double column vector



I'm also able to reproduce Rik's comment #6 examples from bug #50983:


>> for k = zeros (0,3)
  disp ('Hello');
end
whos k
Hello
Hello
Hello
  Name      Size            Bytes  Class     Attributes

  k         0x1                 0  double

>> clear k;
for k = zeros (3, 0)
  disp ('Hello');
end
whos k
  Name      Size            Bytes  Class     Attributes

  k         0x0                 0  double


Nicholas Jankowski <nrjank>
Group Member
Tue 30 Jan 2024 03:27:46 PM UTC, comment #3: 

In Matlab R2023b:

>> for k = zeros (0,3)
  disp ('Hello');
end
Hello
Hello
Hello
>> clear k;
for k = zeros (3, 0)
  disp ('Hello');
end
whos k
  Name      Size            Bytes  Class     Attributes

  k         0x0                 0  double


In Matlab R2016a (the oldest version I have access to):

>> for k = zeros (0,3)
  disp ('Hello');
end
Hello
Hello
Hello
>> clear k;
for k = zeros (3, 0)
  disp ('Hello');
end
whos k
  Name      Size            Bytes  Class     Attributes

  k         0x0                 0  double


Afaict, their behavior didn't change (at least not recently).

Markus Mützel <mmuetzel>
Group administrator
Tue 30 Jan 2024 02:12:10 PM UTC, comment #2: 

Yes, please see comment #6 of bug #50893 where Rik wrote


When an object has columns, but no rows, Octave iterates over the columns while Matlab does not iterate.  Sample code [...]


Was that incorrect, or did Matlab behavior behavior change recently?


I think the fix would be quite simple:


diff --git a/libinterp/parse-tree/pt-eval.cc b/libinterp/parse-tree/pt-eval.cc
--- a/libinterp/parse-tree/pt-eval.cc
+++ b/libinterp/parse-tree/pt-eval.cc
@@ -3187,7 +3187,7 @@ tree_evaluator::visit_simple_for_command
       if (rhs.ndims () > 2)
         arg = arg.reshape (dv);

-      if (nrows > 0 && steps > 0)
+      if (steps > 0)
         {
           octave_value_list idx;
           octave_idx_type iidx;


Whether or not this is a recent change in behavior, but especially if it is, could someone verify the other behavior noted in comment #6 of bug #50893?

John W. Eaton <jwe>
Group administrator
Tue 30 Jan 2024 01:17:08 PM UTC, comment #1: 

Maybe related: bug #50893

If we'd like to change Octave's behavior, it probably needs to be done in the same area.

Just to understand the impact: Do you have a real life example where Octave's behavior causes issues?

Markus Mützel <mmuetzel>
Group administrator
Tue 30 Jan 2024 08:19:58 AM UTC, original submission:  

matlab will happily loop on empty row vectors.

There are quite a few reports on differences between octave and matlab on empty array handling, but I didn't find this one yet. With this example


nc = 4;
z = zeros(0, nc)
z_empty = isempty(z)
for i = z
  a = i
end


octave outputs


z = [](0x4)
z_empty = 1


matlab R2019B output this:


z = 0×4 empty double matrix
z_empty = logical 1
a = 0×1 empty double column vector
a = 0×1 empty double column vector
a = 0×1 empty double column vector
a = 0×1 empty double column vector


A.R. Burgers <arb>

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

Digest:
   bug dependencies.

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by arb (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 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2024-01-30 nrjank StatusNone Duplicate
        Open/ClosedOpen Closed
        Dependencies- Depends on bugs #55622

    Back to the top

    Powered by Savane 3.13-bb6a.
    Corresponding source code