bugGNU Octave - Bugs: bug #61295, cross() dimensions inconsistent...


bug #61295: cross() dimensions inconsistent with Matlab when using mismatched input vector dimensions

Submitted by:  None
Submitted on:  Tue 05 Oct 2021 11:06:16 PM UTC  
Category:  Octave Function Severity:  3 - Normal
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  DavidS Originator Email:  -email is unavailable-
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 07 Oct 2021 05:08:19 PM UTC, comment #1: 

It would probably be better to change the calling code to use all row vectors or all column vectors rather than rely on quirks in the implementation of the cross function.  The Matlab documentation for cross() is available at https://www.mathworks.com/help/matlab/ref/cross.html.  They do not specify what should happen in the case of mixed orientation vectors and thus the behavior could change from version to version.

In fact, this seems to have been the case.  The code for cross() in Octave is just an m-file cross.m.  Looking at the code I see there is a special case for mixed orientation along with a lot of commentary.

  if (ndims (x) < 3 && ndims (y) < 3 && nargin < 3)
    ## COMPATIBILITY -- opposite behavior for cross(row,col)
    ## Swap x and y in the assignments below to get the matlab behavior.
    ## Better yet, fix the calling code so that it uses conformant vectors.
    if (columns (x) == 1 && rows (y) == 1)
      warning ("cross: taking cross product of column by row");
      y = y.';
    elseif (rows (x) == 1 && columns (y) == 1)
      warning ("cross: taking cross product of row by column");
      x = x.';

Matlab doesn't display a lot of consistency.  I would say overall that they have a preference for column vectors, except when they don't.  All of the set functions, for example, prefer column vectors for mixed orientations.

intersect ([1:5], [3:10]')
ans =


But, then you can try something like plot() where the data is specifically a column vector on entry and is transformed into a row vector

h = plot ([1:10]', [1:10]');
get (h, 'xdata')
ans =

    1    2    3    4    5    6    7    8    9   10

get (h, 'ydata')
ans =

    1    2    3    4    5    6    7    8    9   10

You can make your own code conformant by transforming to column vectors using

x = x(:);  % ensure column vector

or to row vectors using

x = x(:).';  % ensure row vector

I've marked the bug as Confirmed, but it will be more expedient to bullet-proof your own code.

Rik <rik5>
Project Administrator
Tue 05 Oct 2021 11:06:16 PM UTC, original submission:  

When calling cross() with a column vector and a row vector or a row vector and a column vector Octave will return a column vector (and a warning) while Matlab will return a row vector.

In octave for example:

> cross([1,2,3]',[0,0,1])
warning: cross: taking cross product of column by row
warning: called from
    cross at line 62 column 7

ans =


 In Matlab:

>> cross([1 2 3]',[0 0 1])

ans =

     2    -1     0

In general, Matlab will only return a column vector if both vectors are column vectors and Octave will only return a row vector if both vectors are row vectors. This can break script compatibility.



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

Attach Files:

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 None (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 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-10-07 rik5 Priority5 - Normal => 3 - Low
        StatusNone => Confirmed
        Release6.3.0 => dev
        Operating SystemMac OS => Any

    Back to the top

    Powered by Savane 3.7