bugGNU Octave - Bugs: bug #42764, squareform in statistics package...


bug #42764: squareform in statistics package returns column vector, Matlab returns row vector

Submitted by:  None
Submitted on:  Sun 13 Jul 2014 04:42:57 PM UTC  
Category: Octave Forge PackageSeverity: 3 - Normal
Priority: 5 - NormalItem Group: Matlab Compatibility
Status: FixedAssigned to: None
Originator Name: nikolaas n. oosterhofOriginator Email: -unavailable-
Open/Closed: ClosedRelease: 3.8.1
Operating System: GNU/Linux

Add a New Comment(Rich Markup)

You are not logged in

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


Sun 09 Nov 2014 03:59:54 PM UTC, comment #4:

Thanks. I applied your second patch to the repository. This bug can be closed.

Arno Onken <asnelt>
Sun 09 Nov 2014 12:20:53 PM UTC, comment #3:

Sorry about that! Just before patching, I reverted the code to what I thought was an earlier version (after testing on MatLab showed that `squareform' should accept both row and column vectors as arguments). I should have retested to make sure all was well.

I ran multiple tests, and ended up rewriting two argument checking lines to use built-in Octave routines (`isnumeric' and `issymmetric'). The rewritten lines should run faster and be easier to maintain. I've attached a new patch `squareform2' with these changes.

(file #32394)

JD Walsh <jdwalsh>
Sat 08 Nov 2014 05:37:02 PM UTC, comment #2:

I tried your patch, but there is a parse error near line 91:

syntax error

>>> if (floor (sy) ~= sy))

I can remove the bracket, but can you apply some additional testing to make sure that there are no other glitches?


Arno Onken <asnelt>
Fri 07 Nov 2014 05:36:34 PM UTC, comment #1:

TL;DR I've attached a patch to bring `squareform' into line with MatLab. With this patch, it tests arguments and returns a row vector (when appropriate).

The lack of argument testing in the current `squareform' creates problems with both vector and matrix input. Currently, `squareform' doesn't complain if the input matrix fails to match the requirements of a distance matrix: a square symmetric matrix with zeros along the diagonal. For example,

`squareform([1,2,3; 4,5,6; 7,8,9])' returns


When `squareform' is asked to convert a vector `x' to an n by n matrix, it finds the value of n by solving the quadratic equation n^2 - n - 2k = 0, where k = length(x). Because the current `squareform' does no error checking, it is possible for `n' to take non-integer values. The function `zeros' which creates the matrix strips off non-integers without displaying an error. This can lead to unexpected results in the returned matrix. For instance,

`squareform ([1,2,3,4])' returns

0 1 2
1 0 3
2 3 0

In both the above cases, MatLab's `squareform' returns an error and aborts computation.

Since the most recent maintainer of `squareform' stepped down in 2011, I have attached a patch for these issues. My patched `squareform' converts matrices to row vectors instead of column vectors. It also checks for incorrect input types, matrices with an incorrect format, and vectors of an invalid length. The MatLab description for `squareform' states that the routine requires a symmetric matrix. However, MatLab 2013b does not throw an error when a non-symmetric matrix is used as input. To maintain compatibility with existing MatLab code which may rely on this lapse, when a non-symmetric matrix is encountered by my version of `squareform', it gives a warning (which the user can choose to turn off).

The patched `squareform' also includes minor formatting changes to bring the code more into line with Octave's current recommendations.

(file #32389)

JD Walsh <jdwalsh>
Sun 13 Jul 2014 04:42:57 PM UTC, original submission:

the squareform function (in the octaveforge statistics package) shows different behaviour in Matlab and Octave when using this function in the 'tovector' direction.

In octave (3.8.1 on linux, 3.8.0 on OSX):

>> squareform(1-eye(3),'tovector')

ans =


In matlab (R2012b on linux, R2013a on OSX):

>> squareform(1-eye(3),'tovector')

ans =

1 1 1

I expected the results to be the same for Matlab and Octave.

I'm not sure to what extent this really is a bug in the sense that is shows behaviour inconsistent with the documentation; neither Matlab's nor Octave's help say explicitly that the output is a row vector. The example given in the Matlab help does however show that the output in Matlab is a row vector.



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

Attach File(s):

Attached Files
file #32394:  squareform2.patch added by jdwalsh (6KiB - text/x-patch)
file #32389:  squareform.patch added by jdwalsh (6KiB - text/x-patch)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -unavailable- added by asnelt (Posted a comment)
  • -unavailable- added by jdwalsh (Updated the item)
  • -unavailable- added by jdwalsh
  • -unavailable- added by None (Submitted the item)

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

    Only project members can vote.


    Please enter the title of George Orwell's famous dystopian book (it's a date):



    Follow 8 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Sun 09 Nov 2014 05:48:58 PM UTCcarandraugStatusNone=>Fixed
    Sun 09 Nov 2014 12:20:53 PM UTCjdwalshAttached File-=>Added squareform2.patch, #32394
    Fri 07 Nov 2014 05:36:34 PM UTCjdwalshAttached File-=>Added squareform.patch, #32389
      Carbon-Copy-=>Added asnelt
    Mon 14 Jul 2014 01:06:32 AM UTCrik5Carbon-CopyRemoved 72865=>-
    Mon 14 Jul 2014 01:06:25 AM UTCrik5CategoryOctave Function=>Octave Forge Package
      SummaryOctave\'s squareform returns column vector, Matlab returns row vector=>squareform in statistics package returns column vector, Matlab returns row vector

    Back to the top

    Powered by Savane 3.1-cleanup1