How to convert a cell array to a symbolic array?

9 views (last 30 days)
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 Comments
gonzalo Mier
gonzalo Mier on 30 Apr 2019
Edited: madhan ravi on 30 Apr 2019
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:
Susan
Susan on 30 Apr 2019
Thanks for your reply. Appreciate that.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Apr 2019
  1. 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.
  2. 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()
  3. 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])
  4. See matlabFunction, and pay close attention to the 'vars' option.
  7 Comments
Walter Roberson
Walter Roberson on 3 May 2019
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.
Susan
Susan on 3 May 2019
Thank you so much for your reply. It was very helpful. Let me see if I can solve the issue with what you suggested. Thanks again

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!