Yekver
Yekver

Reputation: 5185

Matlab dynamic parametres generation

Till now x has two colomns and there was no problems, but now x have got various num of colomns, and I don't know how to write analog code but with dynamic number of colomns in x?

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));

Upvotes: 1

Views: 85

Answers (1)

Amro
Amro

Reputation: 124553

You can use cellarrays to generate a comma separated list:

%# 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) );

Upvotes: 1

Related Questions