MATLAB Answers

Susan
0

How to convert a cell array to a symbolic array?

Asked by Susan
on 29 Apr 2019
Latest activity Commented on by Susan
on 3 May 2019
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

Thanks for your reply.
I am looking for something like you mentioned above, i.e., C = { x1_1, x1_2; x2_1, x2_2} which is a symbolic array of a 2*2 cell in case each cell contains a scalar.
Do you know if I have the size of C for example (3,3,3,10,2) how I can generate the symbolic array of C? In the case that each cell can contain a vector or a matrix, how would the symbolic array be?
Thanks in advance
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:
Thanks for your reply. Appreciate that.

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 30 Apr 2019
 Accepted Answer

  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

Thanks Walter for your reply.
f is a complex function that calls several functions that's why I didn't provide any code for that.
f is a function of (v, p, ptilda, c), so v0 shows up in all steps. In Step 4, I would like to figure out what is optimal v while its initial value is v0.
Since v is a cell array, I don't know how to get its symbolic array to generate the objective function.
You mentioned that fmincon cannot accept a cell array in the initial value location. In this case, the the variable is a cell array, is there any other function that can be used instead of fmincin()?
Thanks in advance
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.
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.