matlab 给出所有组合,matlab-查找多个总和为1的变量的所有组合

我正在尝试解决方程式

x1 + x2 + x3 + .... + xn = 1

其中所有xi的值都限制为[0,0.1,0.2,…,0.9,1].

目前,我正在通过首先生成n维阵列垫来解决该问题,其中每个元素位置的值都是轴值的总和,轴值在axisValues = 0:0.1:1中变化:

mat(i,j,k,...,q) = axisValues(i) + axisValues(j) + ... + axisValues(q).

然后,我搜索结果数组中等于1的所有条目.该代码(如下所示以进行进一步说明)工作正常,并且已针对多达5个维度进行了测试.问题是,运行时间呈指数增长,并且我需要脚本才能用于多个维度.

clear all

dim = 2; % The dimension of the matrix is defined here. The script has been tested for dim ≤ 5

fractiles(:,1) = [0:0.1:1]; % Produces a vector containing the initial axis elements, which will be used to calculate the matrix elements

fractiles = repmat(fractiles,1,dim); % multiplies the vector to supply dim rows with the axis elements 0:0.1:1. These elements will be changed later, but the symmetry will remain the same.

dim_len = repmat(size(fractiles,1),1,size(fractiles,2)); % Here, the length of the dimensions is checked, which his needed to initialize the matrix mat, which will be filled with the axis element sums

mat = zeros(dim_len); % Here the matrix mat is initialized

Sub=cell(1,dim);

mat_size = size(mat);

% The following for loop fills each matrix elements of the dim dimensional matrix mat with the sum of the corresponding dim axis elements.

for ind=1:numel(mat)

[Sub{:}]=ind2sub(mat_size,ind);

SubMatrix=cell2mat(Sub);

sum_indices = 0;

for j = 1:dim

sum_indices = sum_indices+fractiles(SubMatrix(j),j);

end

mat(ind) = sum_indices;

end

Ind_ones = find(mat==1); % Finally, the matrix elements equal to one are found.

我觉得以下使用问题对称性的想法可能有助于大大减少计算时间:

对于2D矩阵,所有满足上述条件的条目都位于从mat(1,11)到mat(11,1)的对角线上,即从x1的最大值到x2的最大值.

对于3D矩阵,所有条目均满足以下条件:通过mat(1,1,11),mat(1,11,1),mat(11,1,1)位于对角平面上,即从x1和x2到x3的最大值.

对于更高的维度也是如此:所有感兴趣的矩阵元素都位于n-1维超平面上,该平面固定在每个维的最高轴值上.

问题是:是否有一种方法可以直接确定这些n-1维超平面上元素的索引?如果是这样,则整个问题可以一步解决,而无需计算n维矩阵的所有条目,然后搜索感兴趣的条目.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值