multi-dimensional symbolic arrays
5 ビュー (過去 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!