How do you overload a built-in function if it has no parameters?
4 ビュー (過去 30 日間)
古いコメントを表示
In order to debug some code, I'm trying to overload the rand and randn functions. I re-arranged some code to better take advantage of vector operations but the liberal use of the rand and randn functions are making it difficult to compare results against the original code. Long story short, I want to create versions of the rand and randn functions that output constant values.
I've had no problem overloading the functions when I specificy output dimensions (e.g. rand(3,1)) but I am at a loss as to how to override the no parameter version which outputs a single scaler. No parameter means no data type to associate the overloaded function with.
0 件のコメント
回答 (2 件)
Jan
2011 年 10 月 27 日
The rand.m file must only appear early in the Matlab path.
function R = rand(varargin)
switch nargin
case 0
R = 3;
case 1
R = repmat(3, varargin{1}, varargin{1});
otherwise
R = repmat(3, [varargin{:}]);
end
Daniel Shub
2011 年 10 月 27 日
Another option would be to reset the random number generator prior to running the original version and then again before running the new version.
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!