How to convert a cell array to a symbolic array?
1 次查看(过去 30 天)
显示 更早的评论
Hey guys,
I have a cell array of size x = cell(max(I(:)), numel(I), numel(I), numel(M), max(M(:))) that each element of this cell array is a matrix. I want to convert it to a symbolic array, and then pass this symbolic array to fmincon(). My objective function is a function of x and I want to min objfun w.r.t. x.
If x was a 2*2 matrix by using x = sym('x', [2 2]) I get
[ x1_1, x1_2]
[ x2_1, x2_2]
But how about the cell array? Any idea?
Thanks in advance.
5 个评论
gonzalo Mier
2019-4-30
编辑:madhan ravi
2019-4-30
I have the feeling that you are not really sure about what you need. But either way, you can always move a symbolic matrix to cell like this:
C{3,3,3,10,2} = {};
C_mat_sym = sym('x', [3,3,3,10,2]);
for i = 1:length(C_mat_sym(:))
C{i} = C_mat_sym(i);
end
And if you need the actual value to hace double as result, use subs:
采纳的回答
Walter Roberson
2019-4-30
- fmincon() does not accept symbolic expressions or functions as any parameter. The only way to pass a symbolic expression or function into fmincon is to parameterize a function, passing the value in as an extra parameter http://www.mathworks.com/help/matlab/math/parameterizing-functions.html . The symbolic expression or function can never be used as x0 or A or b or Aeq or beq or lb or ub, and f and nonlcon require function handles not symbolic values.
- Your objective function for fmincon must return something scalar and numeric. You can pass in symbolic values as extra parameters by parameterizing, but whatever computation you do must return scalar single() or double()
- You indicate that each element of the cell array contains a matrix and that you want to convert the cell to symbolic. However, symbolic arrays cannot contain sub-arrays or vectors or cells: symbolic arrays must have each element be a scalar symbolic value and the array overall must be cuboid. If you knew that each cell entry was the same size, then you could cat() everything along higher dimensions after some reshape() [permute would probably be easier])
- See matlabFunction, and pay close attention to the 'vars' option.
7 个评论
Walter Roberson
2019-5-3
No. In every optimizer that MATLAB provides, the variables being optimized and the initial value must be numeric. Details vary about whether the initial value must be a row vector or column vector or can be a 2D array, but numeric is the only possibility.
Ah, wait... there just might be some optimizer hidden in MuPAD if you knew exactly what to ask for using feval(symengine) or evalin(symengine), and in that case the initial conditions would be symbolic numbers rather than pure numeric. But not cell.
If your v0 is an N x M cell, each member of which is a P x Q matrix, then cell2mat(v0) and pass that or that reshaped to a vector. But this requires that some content having been written into v0.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!