# multi-dimensional symbolic arrays

14 views (last 30 days)
Paul Fishback on 5 Feb 2012
I'm trying to create a three-dimensional array of symbolic variables for purposes of formulating and solving an integer programming problem. I need symbolic entries so that I can perform operations using the symbolic toolbox.
I have no problems with a two-dimensional array:
x=sym('x%d%d',[4,4]);
This creates a 4-by-4 symbolic array.
Can this be modified slightly to get an array in three indices?

Walter Roberson on 5 Feb 2012
Sorry, no, that syntax is pretty new in MATLAB (R2010b), and only supports vectors or 2 dimensional arrays. For a multi-dimensional work-around, please see http://www.mathworks.com/matlabcentral/answers/2521-creating-vector-of-symbols

Andrei Bobrov on 5 Feb 2012
m = 3;
n = 4;
q = 2;
[i1 i2 i3] = ndgrid(1:m,1:n,1:q);
k = cellfun(@sym,regexp(sprintf('x%d_%d_%d',[i1(:) i2(:) i3(:)]'),...
'x[0-9]_[0-9]_[0-9]','match'),'un',0);
x = reshape([k{:}],m,n,[])
OR
m = 3;
n = 4;
q = 2;
[i1 i2 i3] = ndgrid(1:m,1:n,1:q);
k = arrayfun(@(j1)sym(sprintf('x_%d_%d_%d',i1(j1),i2(j1),i3(j1))),...
1:numel(i1),'un',0);
x = reshape([k{:}],m,n,[])