Sun 18 Feb 2018 07:59:53 PM UTC, comment #10:
Another function that should support 'like' functionality
cast

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)

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.

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.

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 musthave 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.');
end
varargin{k} = class(varargin{k+1});
varargin(k+1) = [];
break;
end
end
x = builtin('zeros',varargin{:});

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.

Sun 23 Apr 2017 08:23:30 PM UTC, comment #4:
Looks like not.

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?

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.

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 matrixproducing 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.

Sat 22 Apr 2017 04:34:37 PM UTC, original submission:
MATLAB introduced the 'like' keyword for zeros, ones, cast.
