bugGNU Octave - Bugs: bug #50854, zeros: like keyword


bug #50854: zeros: like keyword

Submitted by:  Ceral Paquet <octavebugs>
Submitted on:  Sat Apr 22 16:34:37 2017  
Category: Octave FunctionSeverity: 1 - Wish
Priority: 2Item Group: WTF, Matlab?!?
Status: ConfirmedAssigned to: None
Originator Name: Open/Closed: Open
Release: 4.2.1Operating System: GNU/Linux

Add a New Comment (Rich MarkupRich Markup):

You are not logged in

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


(Jump to the original submission Jump to the original submission)

Wed Apr 26 19:20:52 2017, comment #7:

Matlab also uses 'like' for rand, randi, randn,
to get a result which has the same object type as the given parameter.

Avinoam Kalma <avinoam>
Project Member
Wed Apr 26 05:55:57 2017, comment #6:

It looks like "like" only copies the following properties:

  • data type (class)
  • complexity
  • sparsity

Sorry I have no ability to implement this properly. It's not that it's a must-have thing, it just not having it breaks Matlab code. This is a horrible hackaround.

function x = zeros(varargin)

for k = 1:nargin
if isequal(varargin{k},'like')
if k+1 > nargin
error('''like'' must be followed by a variable.');
varargin{k} = class(varargin{k+1});
varargin(k+1) = [];

x = builtin('zeros',varargin{:});

Ceral Paquet <octavebugs>
Mon Apr 24 03:38:20 2017, comment #5:

Okay, so they are inconsistent. If you're very interested in this feature you could work out the exact logic they are using and submit a changeset.

Rik <rik5>
Project Administrator
Sun Apr 23 20:23:30 2017, comment #4:

Looks like not.

Ceral Paquet <octavebugs>
Sun Apr 23 19:14:12 2017, comment #3:

I see. Yes, I suppose it would be interesting to have a function that copies all attributes. Does Matlab, for example, also copy the global attribute?

Rik <rik5>
Project Administrator
Sun Apr 23 18:45:19 2017, comment #2:

You're right Rik, class is a nice workaround.

Unfortunately I've got myself addicted to using the gpuArray class in Matlab and 'like' is useful for copying that attribute over. It's still a hack in Matlab if you just want to copy the GPU part but not the complexity... they haven't really ironed out bugs in pcg, lsqr, etc. which use 'like' to declare arrays like resvec. My data is complex so these come back as complex arrays. Matlabs support could not care less, just cut&paste replies describing memory management on the gpu side.

So you still end up with if/else statements all over the place. Annoying.

Ceral Paquet <octavebugs>
Sun Apr 23 14:19:43 2017, comment #1:

Yeah. Matlab has been this way for a while. I happen to think it clutters the interface to the matrix-producing functions like zeros, ones, etc., but maybe it will get implemented.

A pretty easy hack, if one really needs this functionality, is to use class().

The complex attribute isn't really necessary to copy because Octave adjusts matrices automatically as necessary to save memory. If a matrix has all zero imaginary parts t is automatically reduced to real to save memory. Conversely, if you assign a complex variable to a real matrix Octave will conver the matrix to complex. Using the example above.

Now B is taking up 8 bytes just as the Matlab variable does.

And now it is back to requiring only 4 bytes.

Rik <rik5>
Project Administrator
Sat Apr 22 16:34:37 2017, original submission:

MATLAB introduced the 'like' keyword for zeros, ones, cast.

Ceral Paquet <octavebugs>


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

Attach File(s):

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -unavailable- added by avinoam (Posted a comment)
  • -unavailable- added by rik5 (Posted a comment)
  • -unavailable- added by octavebugs (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 4 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Sun Apr 23 14:19:43 2017rik5CategoryNone=>Octave Function
      Severity3 - Normal=>1 - Wish
      Priority5 - Normal=>2

    Back to the top

    Powered by Savane 3.1-cleanup1