Matlab动态参数生成

时间:2011-08-17 21:10:14

标签: optimization matlab

直到 x 有两个colomns且没有问题,但现在 x 有多个colomns,我不知道如何编写模拟代码但是 x 中的动态colonns数量?

min_x = min(x);
max_x = max(x);
step = (max_x - min_x)/50;
[X, Y] = ndgrid(min_x(1):step(1):max_x(1), min_x(2):step(2):max_x(2));

1 个答案:

答案 0 :(得分:1)

您可以使用cellarray生成以逗号分隔的列表:

%# sample data
x = rand(10,3);       %# you can change the column numbers here

%# calculate step sizes
mn = min(x);
mx = max(x);
step = (mx-mn)/50;

%# vec{i} = mn(i):s(i):mx(i)
vec = arrayfun(@(a,s,b)a:s:b, mn,step,mx, 'UniformOutput',false);

%# [X,Y,...] = ndgrid(vec{1},vec{2},...)
C = cell(1,numel(vec));
[C{:}] = ndgrid( vec{:} );

%# result = [X(:),Y(:),...]
result = cell2mat( cellfun(@(v)v(:), C, 'UniformOutput',false) );