Parameter permutations (i.e. Cartesian product) into a multidimensional array

I'm interested in calculating a function on a bunch of permutations of parameter values. I want to keep it common to N dimensions, but let me write it in three dimensions. Generating permutations is easy enough with a meshgrid , but I can't figure out how to change the resulting array to multidimensionality? Here is the starting point:

 %These are the 3 variations of parameters, with some values. params1 = [100, 200, 300];%Picking these so it is easy to correlate to the function params2 = [10, 20]; params3 = [1, 2]; %This generates parameter_values as the cartesian productpermutations. [vec1, vec2, vec3] = meshgrid(params1, params2, params3); parameter_values = [vec1(:) vec2(:) vec3(:)]; %Calculates functions on the set of parameters. %Would have a fancier function, of course, this just makes it easy to see the results. raw_vals = parameter_values(:,1) + parameter_values(:,2) + parameter_values(:,3); %Rearrange into a multiarray to access by parameter indices. f_vals = reshape(raw_vals, [length(params1), length(params2), length(params3)]) %WRONG? %THE FOLLOWING FAIL BUT WOULD BE EXPECTED WITH THESE PARAMETERS AND THE FUNCTION. assert(f_vals(2,1,1) == 211) assert(f_vals(3,2,2) == 322) 
+2
matlab cartesian-product combinatorics
Nov 15 '13 at 0:31
source share
1 answer

You want ndgrid instead of meshgrid in this case.

Syntax

meshgrid [X,Y] = meshgrid(xgv,ygv) , which causes Y(:) change faster, rather than X(:) . For more information, see grid view . In other words, you get

 >> [vec1, vec2, vec3] = meshgrid(params1, params2, params3) vec1(:,:,1) = 100 200 300 100 200 300 vec1(:,:,2) = 100 200 300 100 200 300 vec2(:,:,1) = 10 10 10 20 20 20 vec2(:,:,2) = 10 10 10 20 20 20 ... 

But you want to receive:

 >> [vec1, vec2, vec3] = ndgrid(params1, params2, params3) vec1(:,:,1) = 100 100 200 200 300 300 vec1(:,:,2) = 100 100 200 200 300 300 vec2(:,:,1) = 10 20 10 20 10 20 vec2(:,:,2) = 10 20 10 20 10 20 ... 

If you switch to ndgrid , you will get f_vals(2,1,1) == 211 as intended.

A generalization of N-measures can be performed as follows:

  params = {[100, 200, 300],[10, 20],[1, 2]}; vecs = cell(numel(params),1); [vecs{:}] = ndgrid(params{:}); parameter_values = reshape(cat(numel(vecs)+1,vecs{:}),[],numel(vecs)); raw_vals = sum(parameter_values,2); f_vals = reshape(raw_vals,cellfun(@numel,params)) 
+4
Nov 15 '13 at 1:24
source share
— -



All Articles