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 Function Severity:  1 - Wish
Priority:  2 Item Group:  WTF, Matlab?!?
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  Open
Release:  4.2.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.


( Jump to the original submission)

Sun 17 Mar 2019 01:19:39 AM UTC, comment #14:

Bumpity bump. Another version another request for the like keyword ;)

Ceral Paquet <octavebugs>
Tue 23 Oct 2018 01:18:59 PM UTC, comment #13:

Hi there as per comment #12 I worked on this a long time ago. I was discouraged by the speed for which I got feedback on my patch so I did not continue.

I don't currently have a Octave dev environment so if anyone wants to take my patch and replay it thats fine or they could start from scratch.

Brad Kennedy <co60ca>
Tue 23 Oct 2018 10:43:45 AM UTC, comment #12:

I tried to test the patch from comment #9 but it would need to be adjusted for current dev. Brad, do you think you would have time to update your patch? I can try to have a look otherwise.
It would be great to have it for Octave 5.0.

Guillaume <gyom>
Sun 12 Aug 2018 07:00:26 PM UTC, comment #11:

Just a little bump for the 'like' keyword which is still unsupported (release 4.4.1).

Ceral Paquet <octavebugs>
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 Files:

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
  • -email is unavailable- added by gyom (Posted a comment)
  • -email is unavailable- added by co60ca (Posted a comment)
  • -email is unavailable- added by avinoam (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by octavebugs (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 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-07-12 co60ca Attached File- => Added patch.txt, #41183
    2017-04-23 rik5 CategoryNone => Octave Function
        Severity3 - Normal => 1 - Wish
        Priority5 - Normal => 2
        StatusNone => Confirmed

    Back to the top

    Powered by Savane 3.4