Matlab - 生成输入及其总和的组合
Matlab - generating combinations of input and their sum
我必须制作一个接受 3 个输入的函数,例如 e0
、e1
和 e2
。该函数将有 2 个输出 x
和 y
。
x
将是 e0
、e1
和 e2
的组合。 y
将是一个包含 x
.
列总和的列向量
创建函数必须满足以下条件:
- 输入
e0
、e1
和 e2
各有一个数字。
- 如果用户未输入值,则默认设置为 0。
- 如果用户未输入任何内容,则应显示一条消息,说明未输入任何内容。
这是一个例子:
combination pattern of X (first 3 columns): pattern for y is the sum of x
1 1 1 3
2 1 1 4
3 1 1 5
1 2 1 4
2 2 1 5
3 2 1 6
1 3 1 5
2 3 1 6
and so on... and so on....
到目前为止,我只能通过单独显示 x
和 y
来完成这些工作。
function [x,y]=create(e0,e1,e2)
switch nargin
case 2
e1=0;
e2=0;
case 1
e2=0;
case 0
disp('no input')
end
我用谷歌搜索了我的问题,发现 combvec 和 allcomb 应该有帮助,但我不知道如何......
请帮助任何答案或提示将是一个很大的帮助。
您想要的是排列,而不是组合。您列出 2 3 1
和 3 2 1
是不同的,但是如果您要将其组合,您会认为它们是相同的。因此,我将转而向您推荐 post:
How to find all permutations (with repetition) in MATLAB?
我特意采纳Rody Oldenhuis的回答。因此,您可以通过以下方式构造所有可能的排列:
x = unique(nchoosek(repmat([e0 e1 e2], 1, 3), 3), 'rows');
这将创建一个包含 e0
e1
和 e2
的所有可能排列的数组。因此,使用带有 e0 = 1, e1 = 2, e2 = 3
的示例,我们得到:
x =
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
现在终于得到你想要的,你只需要对行求和,这样做:
y = sum(x, 2)
y =
3
4
5
4
5
6
5
6
7
4
5
6
5
6
7
6
7
8
5
6
7
6
7
8
7
8
9
您已经编写了当用户未为 e0
、e1
and/or e2
输入任何内容时要处理的案例,因此您只需要做的就是获取您的代码,并将我上面写的内容拼凑到您的函数中。它应该给你想要的。
您 allcomb
的方向是正确的。您实际上是在寻找 cartesian product:
[e0, e1, e2] x [e0, e1, e2] x [e0, e1, e2] == [e0, e1, e2]^3.
不过你不需要 allcomb
,因为 ndgrid
可以 already do this。
让我们从您的代码开始。它有一个错误。基本上 case 2
和 case 1
被翻转了。
function [x,y] = create(e0,e1,e2)
switch nargin
case 2
e1=0; % <- When nargin==2, this value is set, and you overwrite it.
e2=0;
case 1
e2=0; % <- When nargin==1, e1 must also be set to zero.
case 0
disp('no input') % <- use `error` instead of `disp`? If not, use `return` here.
end
然后您需要检查提供的数字是否不是矩阵。这可以通过类似的东西来实现。 (填空。)
assert(numel(e1)==1 && numel(__)___ && numel(__)___,'Input sizes are incorrect');
要生成您正在寻找的笛卡尔积 [e0, e1, e2] x [e0, e1, e2] x [e0, e1, e2]
,您可以使用 this answer for allcomb
or this answer 作为内置 ndgrid
。
sets = {[e0,e1,e2], [e0,e1,e2], [e0,e1,e2]};
cartProd1 = allcomb(sets{:})
[x y z] = ndgrid(sets{:});
cartProd2 = [x(:) y(:) z(:)]
如果您想要正确的顺序,您可以交换 cartProd
的列。
要沿行生成总和,请使用
sum(cartProd,2)
我必须制作一个接受 3 个输入的函数,例如 e0
、e1
和 e2
。该函数将有 2 个输出 x
和 y
。
x
将是 e0
、e1
和 e2
的组合。 y
将是一个包含 x
.
创建函数必须满足以下条件:
- 输入
e0
、e1
和e2
各有一个数字。 - 如果用户未输入值,则默认设置为 0。
- 如果用户未输入任何内容,则应显示一条消息,说明未输入任何内容。
这是一个例子:
combination pattern of X (first 3 columns): pattern for y is the sum of x
1 1 1 3
2 1 1 4
3 1 1 5
1 2 1 4
2 2 1 5
3 2 1 6
1 3 1 5
2 3 1 6
and so on... and so on....
到目前为止,我只能通过单独显示 x
和 y
来完成这些工作。
function [x,y]=create(e0,e1,e2)
switch nargin
case 2
e1=0;
e2=0;
case 1
e2=0;
case 0
disp('no input')
end
我用谷歌搜索了我的问题,发现 combvec 和 allcomb 应该有帮助,但我不知道如何...... 请帮助任何答案或提示将是一个很大的帮助。
您想要的是排列,而不是组合。您列出 2 3 1
和 3 2 1
是不同的,但是如果您要将其组合,您会认为它们是相同的。因此,我将转而向您推荐 post:
How to find all permutations (with repetition) in MATLAB?
我特意采纳Rody Oldenhuis的回答。因此,您可以通过以下方式构造所有可能的排列:
x = unique(nchoosek(repmat([e0 e1 e2], 1, 3), 3), 'rows');
这将创建一个包含 e0
e1
和 e2
的所有可能排列的数组。因此,使用带有 e0 = 1, e1 = 2, e2 = 3
的示例,我们得到:
x =
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
现在终于得到你想要的,你只需要对行求和,这样做:
y = sum(x, 2)
y =
3
4
5
4
5
6
5
6
7
4
5
6
5
6
7
6
7
8
5
6
7
6
7
8
7
8
9
您已经编写了当用户未为 e0
、e1
and/or e2
输入任何内容时要处理的案例,因此您只需要做的就是获取您的代码,并将我上面写的内容拼凑到您的函数中。它应该给你想要的。
您 allcomb
的方向是正确的。您实际上是在寻找 cartesian product:
[e0, e1, e2] x [e0, e1, e2] x [e0, e1, e2] == [e0, e1, e2]^3.
不过你不需要 allcomb
,因为 ndgrid
可以 already do this。
让我们从您的代码开始。它有一个错误。基本上 case 2
和 case 1
被翻转了。
function [x,y] = create(e0,e1,e2)
switch nargin
case 2
e1=0; % <- When nargin==2, this value is set, and you overwrite it.
e2=0;
case 1
e2=0; % <- When nargin==1, e1 must also be set to zero.
case 0
disp('no input') % <- use `error` instead of `disp`? If not, use `return` here.
end
然后您需要检查提供的数字是否不是矩阵。这可以通过类似的东西来实现。 (填空。)
assert(numel(e1)==1 && numel(__)___ && numel(__)___,'Input sizes are incorrect');
要生成您正在寻找的笛卡尔积 [e0, e1, e2] x [e0, e1, e2] x [e0, e1, e2]
,您可以使用 this answer for allcomb
or this answer 作为内置 ndgrid
。
sets = {[e0,e1,e2], [e0,e1,e2], [e0,e1,e2]};
cartProd1 = allcomb(sets{:})
[x y z] = ndgrid(sets{:});
cartProd2 = [x(:) y(:) z(:)]
如果您想要正确的顺序,您可以交换 cartProd
的列。
要沿行生成总和,请使用
sum(cartProd,2)