multi-dimensional symbolic arrays
4 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Walter Roberson
2012-2-5
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
0 个评论
Andrei Bobrov
2012-2-5
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,[])
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!