如何生成一个矩阵,其中所有可能的数字组合总和为重复的总和?
基本上,组合x1,x2,x3使得x1 + x2 + x3 = n.
例如: n =3
0 1 2
0 2 1
1 0 2
1 2 0
1 1 1
Run Code Online (Sandbox Code Playgroud)
有没有使用预定义的Matlab函数执行此操作的简单方法?
我试过了
n=6;
nchoosek(0:n,3)
Run Code Online (Sandbox Code Playgroud)
这给了我
0 1 2
0 1 3
0 1 4
0 1 5
0 1 6
0 2 3
0 2 4
0 2 5
0 2 6
0 3 4
0 3 5
0 3 6
0 4 5
0 4 …Run Code Online (Sandbox Code Playgroud) 给定矩阵Z(i,j)使得它映射到两个阵列X(i)和Y(j).我试图在一定范围内找到Z的元素(以及相应的X和Y).
我现在使用逻辑索引执行以下操作.鉴于这个例子
X = 1:5;
Y = 1:5;
Z = [17 24 1 8 15
23 5 6 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9]
Z((X>1 & X<4),Y==3)
Run Code Online (Sandbox Code Playgroud)
这工作正常,但现在我希望找到此特定范围内返回值的最小值,
我在做什么
min(Z((X>1 & X<4),Y==3))
Run Code Online (Sandbox Code Playgroud)
但是现在我如何找回最小值的相应X和Y值?由于我的逻辑索引返回一个数组,到目前为止我尝试的所有方法都返回了答案数组中min的索引,而不是原始的Z矩阵.
我不能用
[row col] = find(Z==min(Z((X>1 & X<4),Y==3)))
Run Code Online (Sandbox Code Playgroud)
因为重复.我有什么选择?
我通过设置将 PowerShell 添加到我的上下文菜单中:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\Powershell\command]
@="powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"
Run Code Online (Sandbox Code Playgroud)
这很有效,除了我巧妙地命名为Ash's docs. 哪个失败了:
该字符串缺少终止符:'。
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullQualifiedErrorId : TerminatorExpectedAtEndOfString
从Set-Location文档:
指定位置的路径。LiteralPath参数的值与键入的值完全相同。没有字符被解释为通配符。如果路径包含转义字符,请将其括在单引号中。单引号告诉 PowerShell 不要将任何字符解释为转义序列。
我是否可以在这里使用-Path并希望我的目录中没有可能包含通配符的目录?或者可以逃脱 中的单引号%V吗?
给出一个矢量
X = [1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3]
Run Code Online (Sandbox Code Playgroud)
我想生成一个这样的矢量
Y = [1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5]
Run Code Online (Sandbox Code Playgroud)
到目前为止,我得到的是
idx = find(diff(X))
Y = [1:idx(1) 1:idx(2)-idx(1) 1:length(X)-idx(2)]
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更优雅(强大)的解决方案?
我有一个8 int,4 positive和4 negative的数组.
X [10,-2,30,-4,5,-20,8,-9]
Run Code Online (Sandbox Code Playgroud)
现在,让我们
Evaluated = a-b+c-d+e-f+g-h
Run Code Online (Sandbox Code Playgroud)
其中a,b..h是从X获取的唯一值.我需要确保这一点
案例1.评估=最接近零.
案例2.通过解决Evaluated来列出最大的可能性.我可以通过对数组进行排序并将最大值分配给a,c,e和g以及最小值b,d,f和h来找到最大值.但如何找到接下来的4个值?
有8个!解决这个等式的方法对吗?
确定此解决方案的最佳方法是什么?
matlab ×3
algorithm ×1
arrays ×1
combinations ×1
escaping ×1
excel ×1
excel-vba ×1
find ×1
matrix ×1
permutation ×1
powershell ×1
registry ×1
vba ×1