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


bug #50854: zeros: like keyword

Submitted by:  Ceral Paquet <octavebugs>
Submitted on:  Sat 22 Apr 2017 04:34:37 PM UTC  
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 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)

Sun 18 Feb 2018 07:59:53 PM UTC, comment #10:

Another function that should support 'like' functionality


Ceral Paquet <octavebugs>
Wed 12 Jul 2017 04:52:40 AM UTC, comment #9:

I added the functionality 'like' functionality to the following functions:
- NaN
- Inf
- ones
- zeros
- false
- true

Which were all listed in Matlab 2017a documentation as accepting the 'like' argument. (and existed in the data.cc file)

I also added tests to this effect.

Patch should be attached. I think we should separate this from rand, randi, randn which localize this patch.

(file #41183)

Brad Kennedy <co60ca>
Tue 11 Jul 2017 04:01:54 AM UTC, comment #8:

Inf and NaN both use this call model too.

I have some of this done and will submit patches for this.

Brad Kennedy <co60ca>
Wed 26 Apr 2017 07:20:52 PM UTC, 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 26 Apr 2017 05:55:57 AM UTC, 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 24 Apr 2017 03:38:20 AM UTC, 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 23 Apr 2017 08:23:30 PM UTC, comment #4:

Looks like not.

Ceral Paquet <octavebugs>
Sun 23 Apr 2017 07:14:12 PM UTC, 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 23 Apr 2017 06:45:19 PM UTC, 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 23 Apr 2017 02:19:43 PM UTC, 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 22 Apr 2017 04:34:37 PM UTC, 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):

Attached Files
file #41183:  patch.txt added by co60ca (6KiB - text/plain)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -unavailable- added by co60ca (Posted a comment)
  • -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 5 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Wed 12 Jul 2017 04:52:40 AM UTCco60caAttached File-=>Added patch.txt, #41183
    Sun 23 Apr 2017 02:19:43 PM UTCrik5CategoryNone=>Octave Function
      Severity3 - Normal=>1 - Wish
      Priority5 - Normal=>2

    Back to the top

    Powered by Savane 3.1-cleanup1