假设我为6位数值,具有一些已知值,如何基于此生成所有可能的数字集?例如:
5 _ 8 _ _ _
因此,MATLAB将生成可能的数字列表:
5 9 8 1 2 5
5 4 8 4 5 1
.
.
.
当然没有任何相同的重复次数。谢谢!
修改
示例代码:
rand1=num2str(5);
rand2=num2str(randi([0 9],[1 1]));
rand3=num2str(8);
rand4=num2str(randi([0 9],[1 1]));
rand5=num2str(randi([0 9],[1 1]));
rand6=num2str(randi([0 9],[1 1]));
final=strcat(rand1,rand2,rand3,rand4,rand5,rand6)
答案 0 :(得分:3)
%Input values
% Column 1: 1 if value already specified, 0 otherwise
% Column 2: Value for specified digits
input_matrix = [
1 5;
0 0;
1 8;
0 0;
0 0;
0 0;
];
n_fixed = length(find(input_matrix(:,1))); %Elements already fixed
n_total = size(input_matrix,1); %Total number of elements
%Generate combinations (not repeated) for values not fixed
comb = unique(nchoosek(repmat(0:9,1,9),n_total-n_fixed),'rows');
n_combs = size(comb,1); %number of combinations
%Insert known values
for i=1:n_total
if(input_matrix(i,1)) %value specified
comb = [comb(:,1:i-1), input_matrix(i,2)*ones(n_combs,1), comb(:,i:end)];
end
end
答案 1 :(得分:0)
这应该有效:
n = 10; % count
d = 6; % digits
res = cell(n,1);
for i = 1:n
x = NaN(1,6);
x(1) = 5;
x(2) = randi([0 9],1,1);
x(3) = 8;
x(4:end) = randi([0 9],1,3);
res{i} = strrep(num2str(x),' ','');
end