combination function using ndgrid

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.

 採用された回答

Guillaume
Guillaume 2016 年 5 月 18 日

0 投票

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 件のコメント

Nik Sam
Nik Sam 2016 年 5 月 18 日
Now is this
[x1,x2,x3]=comb(a,b,c)
x1 =
1
2
1
2
1
2
1
2
x2 =
2
2
3
3
2
2
3
3
x3 =
1
1
1
1
4
4
4
4
Guillaume
Guillaume 2016 年 5 月 18 日
編集済み: Guillaume 2016 年 5 月 18 日
So do you want one output as a matrix, or a variable number of outputs as vectors?
If you just want one output as a matrix, then you misled us with your varargout and the function is simply:
function c = comb(varargin)
c = cell(size(varargin));
[c{:}] = ndgrid(varargin{:};
c = cell2mat(cellfun(@(m) reshape(m, [], 1), c, 'UniformOutput', false)); %reshape into columns and convert to matrix
end
Nik Sam
Nik Sam 2016 年 5 月 18 日
編集済み: Nik Sam 2016 年 5 月 18 日
I want a variable number of outputs because I want after that to compute equation like that
a=[1 2], b=[2 3], c=[1 4] so I want first the combination like
[x1 x2 x3] = comp(a,b,c) to give this:
1 2 1
2 2 1
1 3 1
2 3 1
1 2 4
2 2 4
1 3 4
2 3 4
and after to compute the equation F=x1*(x2-x3) like 1*(2 -1)=1, 2*(2-1)=2 etc.
Is there another way to achieve this? I dont know..
Guillaume
Guillaume 2016 年 5 月 18 日
What you are saying makes no sense. You say you want three outputs (x1, x2, and x3) and then show what looks like a single matrix.
The x1, x2, and x3 returned by my first function are the columns of that matrix. The matrix returned by second function is that matrix.
You can use either form to calculate your F:
%using variable number of outputs:
[x1, x2, x3] = comb(a, b, c); %1st version of comb
F = x1 .* (x2 - x3)
%using single output:
x123 = comb(a, b, c); %2nd version of comb
F = x123(:, 1) .* (x123(:, 2) - x123(:, 3))
Nik Sam
Nik Sam 2016 年 5 月 18 日
編集済み: Nik Sam 2016 年 5 月 18 日
thanks a lot man
But the best of all it will be to have the following:
[x1 x2 x3] = comb(a,b,c)
1 2 1
2 2 1
1 3 1
2 3 1
1 2 4
2 2 4
1 3 4
2 3 4
and after
F=x1.*(x2-x3)
T=min(F)
W=max(F)
IS THERE A WAY FOR THAT?
Nik Sam
Nik Sam 2016 年 5 月 18 日
function varargout = comb( varargin )
varargout = cell(1, nargout);
[varargout{:}] = ndgrid(varargin{:}); %distribute to varargout
varargout = cell2mat(cellfun(@(m) reshape(m, [], 1), varargout, 'UniformOutput', false)); %reshape into columns
end
something like this but it doesnt work
Guillaume
Guillaume 2016 年 5 月 19 日
I'll say again: "What you are saying makes no sense. You say you want three outputs (x1, x2, and x3) and then show what looks like a single matrix".
You can of course concatenate the 3 outputs for display. Using the varargout form of comb:
[x1, x2, x3] = comb(a, b, c);
[x1, x2, x3] %for display
F = x1 .* (x2 - x3)
T = min(F)
W = max(F)
Nik Sam
Nik Sam 2016 年 5 月 19 日
Can I have that display form directly inside the function comb?
Maybe something like
[varargout];
celldisp[varargout];
But it doesnt work like that.
THIS IS MY LAST QUESTION..
Guillaume
Guillaume 2016 年 5 月 19 日
[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.
Nik Sam
Nik Sam 2016 年 5 月 19 日
Really thanks for your time my friend..Now finally I have the desired result..
Thanks again...

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 5 月 18 日

0 投票

[varargout{:}] = ndgrid(varargin{:});

3 件のコメント

Nik Sam
Nik Sam 2016 年 5 月 18 日
I am trying to run this in command window like
a=[1,2];
b=[2,3];
c=[1,4];
[x1 x2 x3] = comp(a,b,c)
but I get this message
Expected one output from a curly brace or dot indexing expression, but there were 0 results.
Error in comp (line 3) [varargout{:}] = ndgrid(varargin{:});
Walter Roberson
Walter Roberson 2016 年 5 月 18 日
function [ varargout ] = comb( varargin )
[varargout{1:nargout}] = ndgrid(varargin{:});
end
Nik Sam
Nik Sam 2016 年 5 月 18 日
This works my friend but if I run the following
>> a=[1,2];
>> b=[2,3];
>> c=[1,4];
>> [x1,x2,x3]=comb(a,b,c)
I get this
x1(:,:,1) =
1 1
2 2
x1(:,:,2) =
1 1
2 2
x2(:,:,1) =
2 3
2 3
x2(:,:,2) =
2 3
2 3
x3(:,:,1) =
1 1
1 1
x3(:,:,2) =
4 4
4 4
But i want as result:
1 2 1
2 2 1
1 3 1
2 3 1
1 2 4
2 2 4
1 3 4
2 3 4

サインインしてコメントする。

Jos (10584)
Jos (10584) 2016 年 5 月 18 日

0 投票

Take a look the content of ALLCOMB, which does exactly what you're after btw...

カテゴリ

ヘルプ センター および File ExchangeGraphics Object Programming についてさらに検索

質問済み:

2016 年 5 月 18 日

コメント済み:

2016 年 5 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by