我们如何使用nchoosek()获得矩阵行的所有组合? [英] How can we use nchoosek() to get all the combinations of the rows of a matrix?

查看:123
本文介绍了我们如何使用nchoosek()获得矩阵行的所有组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有1到5个数字的向量v,则可以使用nchoosek(v,2)来获得具有两个元素的所有组合.但是,此函数现在允许我们获取矩阵的所有组合.我想用它来获取矩阵行的所有组合.

If we have a vector v of 1- 5 numbers we can use nchoosek(v,2) to get all the combinations having two elements. But this function does now allow us to get all the combinations of a matrix. I want to use it to get all the combinations of rows of a matrix.

推荐答案

这里是一种方法:

function p = q47204269(inMat)
% Input handling:
if nargin == 0 || isempty(inMat)
  inMat = magic(5);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rowsCell = num2cell(inMat,2);
nRows = size(inMat,1);
p = cell(nRows,1);
for indR = 1:nRows
  r = nchoosek(1:nRows,indR);
  p{indR} = cell2mat(reshape(rowsCell(r.',:).',indR,1,[]));
end  

另请参阅:

  • The perms function, as it might come in handy in what you're doing.
  • This question.

这篇关于我们如何使用nchoosek()获得矩阵行的所有组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆