如何使用矢量化代码在 MATLAB 中从两个向量生成所有对?
现在我不止一次需要在 MATLAB 中生成所有可能的两个向量对,我使用 for 循环来执行此操作,这会占用相当多的代码行,即
vec1 = 1:4;
vec2 = 1:3;
i = 0;
pairs = zeros([4*3 2]);
for val1 = vec1
for val2 = vec2
i = i + 1;
pairs(i,1) = val1;
pairs(i,2) = val2;
end
end
生成...
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
必须有一种更好的方法来执行此操作,这比 MATLAB 更有效'式?
nb nchoosek
不执行我需要的反转对(即 2 1
以及 1 2
),我不能只是反转并附加 nchoosek
输出,因为对称对将被包含两次。
More than once now I have needed to generate all possible pairs of two vectors in MATLAB which I do with for loops which take up a fair few lines of code i.e.
vec1 = 1:4;
vec2 = 1:3;
i = 0;
pairs = zeros([4*3 2]);
for val1 = vec1
for val2 = vec2
i = i + 1;
pairs(i,1) = val1;
pairs(i,2) = val2;
end
end
Generates ...
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
There must be a better way to do this which is more MATLAB'esque?
n.b. nchoosek
does not do the reversed pairs which is what I need (i.e. 2 1
as well as 1 2
), I can't just reverse and append the nchoosek
output because the symmetric pairs will be included twice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
尝试
查看 MESHGRID 文档。虽然这并不完全是该函数的用途,但如果你仔细看看它,你会发现你所要求的正是它的作用。
Try
See the MESHGRID documentation. Although this is not exactly what that function is for, but if you squint at it funny, what you are asking for is exactly what it does.
您可以使用
You may use
您可以通过使用
repmat
复制矩阵,然后使用reshape
将结果转换为列向量来实现。当然,您可以删除上面示例中的所有中间变量。
You could do it by replicating the matrices using
repmat
and then turning the result into a column vector usingreshape
.Of course, you can get rid of all the intermediate variables from the example above.
另一种收集解决方案:
Another solution for collection:
您正在寻找的是 笛卡尔积
cartprod 是实现它的函数。您可以在线性代数包中找到它,因此您需要执行以下操作:
What you are looking for is the cartesian product
cartprod is the function that implements it. You can find it in the linear-algebra package so you would need to do:
您可以使用普通的旧矩阵运算,例如在
编辑:中,这是Octave语法,MATLAB将如下所示:
在这两种情况下,结果将是
You can use plain old matrix operations, e.g. in
Edit: this is Octave syntax, MATLAB will look like this:
in both cases, result will be
从 MATLAB R2023a 开始,您可以使用组合函数来执行此操作。详细信息请参见 https://blogs.mathworks.com/matlab/2023/04/04/the-new-combinations-function-in-matlab-for-cartesian-products-and-parameter-sweeps/
结果是一个表。当所有数据类型兼容时(就像这里的情况),你可以得到这样的矩阵
As of MATLAB R2023a, you can use the combinations function to do this. Details at https://blogs.mathworks.com/matlab/2023/04/04/the-new-combinations-function-in-matlab-for-cartesian-products-and-parameter-sweeps/
The result is a table. When all data types are compatible (as is the case here) you can get the matrix like this
这里有一种更类似于 MATLAB 的方法来查找组合。这个也可以很容易地扩展到超过 2 个向量(以及非数字组合):
Here a more MATLAB'esque way to find the combinations. This one can also be easily extended to more then 2 vectors (and also non-numerical combinations):
从版本 R2015a 开始,您可以使用
repelem
来执行此操作 和repmat
:这种类型的解决方案避免了某些其他解决方案(例如基于
meshgrid
),这可能会导致较大向量的内存问题。Starting from version R2015a, you can do this using
repelem
andrepmat
:This type of solution avoids the additional intermediate variables required by some of the other solutions (such as those based on
meshgrid
) which could lead to memory issues for larger vectors.