bugGNU Octave - Bugs: bug #61705, Wrong number of columns when...

 
 

bug #61705: Wrong number of columns when removing entry from empty vector

Submitted by:  Vassilis Virvilis <vasvir>
Submitted on:  Sun 19 Dec 2021 05:18:14 PM UTC  
 
Category:  Interpreter Severity:  2 - Minor
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  vasvir Open/Closed:  Open
Release:  dev Operating System:  Any

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Thu 23 Dec 2021 12:19:56 AM UTC, comment #3: 

This is a minor nuisance, and Matlab isn't much better.  To quickly summarize, you cannot rely on columns() when a matrix is empty.  If your code will encounter empty matrices you may need to separate them and use different code for them in a special case.  You can separate them using isempty() or "if (numel (x) == 0)".

For a slightly longer explanation, both Matlab and Octave allow you to create arbitrarily-sized N-dimensional matrices.  And you can request the number of columns for these matrices regardless of whether they actually hold anything.  Here's some code that shows that:

x = zeros (0, 101, 5)
x = [](0x101x5)
columns (x)
ans = 101

Now if you do indexing the result is the size of the index.  This applies even to empty indexing.

x = 1:10;
idx = zeros (0,3)
idx = [](0x3)
y = x(idx)
y = [](0x3)

Of course, this is where it gets silly/stupid.  The expression "x != 2" should be of size 0x0.  It is when it is executed directly, but not when it is executed as part of an indexing operation.

x = []
x = [](0x0)
x ~= 2
ans = [](0x0)
x(x ~= 2)
ans = [](0x1)

I've lowered the severity since this is a minor issue that can be more appropriately handled by detecting empty matrices.  I've also changed the Item Group to Matlab Compatibility since it seems to generate a 0x0 matrix for the test case.

Rik <rik5>
Project Administrator
Sun 19 Dec 2021 08:40:18 PM UTC, comment #2: 

Hi I don't get "invalid operator" for != in 6.2 linux

Looks like ~= and != are the same.

It is even documented at https://octave.org/doc/v6.4.0/Comparison-Ops.html#Comparison-Ops

I didn't see you replicating the problem in the console output you posted. Maybe I missed something...

Here is the minimal way to replicate the problem.

octave:6> vector = []
vector = [](0x0)
octave:7> columns(vector)
ans = 0                <---------------- OK

# Now let's remove a non existent entry from a zero size vector

octave:8> vector = vector(vector != 2)
vector = [](0x1)
octave:9> columns(vector)
ans = 1                <---------------- ????

Vassilis Virvilis <vasvir>
Sun 19 Dec 2021 05:57:34 PM UTC, comment #1: 

This is still the same with a build from the current default branch (hg id 6ffa6dbbf42a), and also occurs with 6.4.0 on Windows 10.

With Matlab R2021a:

>> vector = []

vector =

     []

>> size(vector)

ans =

     0     0

>> vector = vector(vector != 2)
 vector = vector(vector != 2)
                        ↑
Invalid use of operator.

Did you mean:
>> vector = vector(vector ~= 2)

vector =

     []

>> size(vector)

ans =

     0     0

>> vector = [2]

vector =

     2

>> vector = vector(vector ~= 2)

vector =

     []

>> size(vector)

ans =

     0     0

Markus Mützel <mmuetzel>
Project Member
Sun 19 Dec 2021 05:18:14 PM UTC, original submission:  

Hi,

Debian unstable here with octave 6.2.0

Is the following behavior correct? I would expect columns to be zero.

octave:6> vector = []
vector = [](0x0)
octave:7> columns(vector)
ans = 0                <---------------- OK
octave:8> vector = vector(vector != 2)
vector = [](0x1)
octave:9> columns(vector)
ans = 1                <---------------- ????

If there is something to remove everything is ok

octave:10> vector = [2]
vector = 2
octave:11> columns(vector)
ans = 2
octave:12> vector = vector(vector != 2)
vector = [](0x0)
octave:7> columns(vector)
ans = 0                <---------------- OK

This seems inconsistent and I thought it would be best to report it. I tried to search if a bug like this has been reported but I didn't find anything.

Hope that helps.

   Vassilis

Vassilis Virvilis <vasvir>

 

(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 rik5 (Posted a comment)
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by vasvir (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 7 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-12-23 rik5 Severity3 - Normal => 2 - Minor
        Priority5 - Normal => 3 - Low
        Item GroupIncorrect Result => Matlab Compatibility
    2021-12-19 mmuetzel StatusNone => Confirmed
        Release6.2.0 => dev
        Operating SystemGNU/Linux => Any
    2021-12-19 mmuetzel Item GroupNone => Incorrect Result

    Back to the top


    Powered by Savane 3.7