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

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

[Octave-bug-tracker] [bug #61295] cross() dimensions inconsistent with M


From: Rik
Subject: [Octave-bug-tracker] [bug #61295] cross() dimensions inconsistent with Matlab when using mismatched input vector dimensions
Date: Thu, 7 Oct 2021 13:08:20 -0400 (EDT)
User-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36

Update of bug #61295 (project octave):

                Priority:              5 - Normal => 3 - Low                
                  Status:                    None => Confirmed              
                 Release:                   6.3.0 => dev                    
        Operating System:                  Mac OS => Any                    

    _______________________________________________________

Follow-up 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.';
    endif
  endif


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 =

   3
   4
   5


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.

    _______________________________________________________

Reply to this item at:

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

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




reply via email to

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