我想func(par1,par2,par3)
用参数的所有组合测试一个函数par1
,par2
并将par3
输出存储在一个.mat
文件中。我的代码现在看起来像这样:
n1 = 3;
n2 = 1;
n3 = 2;
parList1 = rand(1,n1); % n1,n2,n3 is just some integer
parList2 = rand(1,n2); % the lists are edited by hand in the actual script
parList3 = rand(1,n3);
saveFile = matfile('file.mat','Writable',true);
% allocate memory
saveFile.output = NaN(numel(parList1),numel(parList2),numel(parList3));
counter1 = 0;
for par1 = parList1
counter1 = counter1 + 1;
counter2 = 0; % reset inner counter
for par2 = parList2
counter2 = counter2 + 1;
counter3 = 0; % reset inner counter
for par3 = parList3
counter3 = counter3 + 1;
saveFile.output(counter1,counter2,counter3) = sum([par1,par2,par3]);
end
end
end
这有效,除非 ifparList3
只有一项,即 if n3 = 1
。然后saveFile.output
有单身维度,我得到了错误
Variable 'output' has 2 dimensions in the file, this does not match the 3 dimensions in the indexing subscripts.
有没有优雅的方法来解决这个问题?