combination function using ndgrid
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nik Sam
am 18 Mai 2016
Kommentiert: Nik Sam
am 19 Mai 2016
Hello,
I want to build a function which use ndgrid.
My function is
function [ varargout ] = comb( varargin )
[varargout]=ndgrid(varargin)
varargout=varargout(:);
disp([varargout])
end
My varargout are x1 x2 x3 and varargin are a,b,c where a=[1 2], b=[2 3] and c=[1 4]. Expected results are
1 2 1
2 2 1
1 3 1
2 3 1
1 2 4
2 2 4
1 3 4
2 3 4
but it seems like ndgrid doesn't work. What can i do? In command window works fine but as a function where I can put more varargins something is wrong.
0 Kommentare
Akzeptierte Antwort
Guillaume
am 18 Mai 2016
function varargout = comb(varargin)
varargout = cell(1, nargout);
[varargout{:}] = ndgrid(varargin{:}; %distribute to varargout
varargout = cellfun(@(m) reshape(m, [], 1), varargout, 'UniformOutput', false); %reshape into columns
end
10 Kommentare
Guillaume
am 19 Mai 2016
[varargout{:}]
at the end of the function will do it. This will always display the output whether or not you terminate the call to comb with a semicolon, so I wouldn't recommend it.
There is no way to detect if a function has called with a semicolon or not, so you can't toggle that behaviour.
Weitere Antworten (2)
Walter Roberson
am 18 Mai 2016
[varargout{:}] = ndgrid(varargin{:});
3 Kommentare
Walter Roberson
am 18 Mai 2016
function [ varargout ] = comb( varargin )
[varargout{1:nargout}] = ndgrid(varargin{:});
end
Jos (10584)
am 18 Mai 2016
Take a look the content of ALLCOMB, which does exactly what you're after btw...
0 Kommentare
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!