How can I convert a cell array to complex double?

조회 수: 6 (최근 30일)
Luis Morales
Luis Morales 2019년 1월 5일
댓글: Luis Morales 2019년 1월 5일
Hi everyone, I am new using matlab, I did some calculation using sym in order to get a complex matrix, but I need to conver this matrix to a complex double matrix, how can I do that?
gg =
25×1 cell array
{'z(2)*2i - z(6)*2i' }
{'z(1)*2i + z(3)*2i - z(7)*2i' }
{'z(2)*2i + z(4)*2i - z(8)*2i' }
{'z(3)*2i + z(5)*2i - z(9)*2i' }
{'z(4)*2i - z(10)*2i' }
{'z(7)*2i - z(1)*2i - z(11)*2i' }
{'z(6)*2i - z(2)*2i + z(8)*2i - z(12)*2i' }
{'z(7)*2i - z(3)*2i + z(9)*2i - z(13)*2i' }
{'z(8)*2i - z(4)*2i + z(10)*2i - z(14)*2i' }
{'z(9)*2i - z(5)*2i - z(15)*2i' }
{'z(12)*2i - z(6)*2i - z(16)*2i' }
{'z(11)*2i - z(7)*2i + z(13)*2i - z(17)*2i' }
{'z(12)*2i - z(8)*2i + z(14)*2i - z(18)*2i' }
{'z(13)*2i - z(9)*2i + z(15)*2i - z(19)*2i' }
{'z(14)*2i - z(10)*2i - z(20)*2i' }
{'z(17)*2i - z(11)*2i - z(21)*2i' }
{'z(16)*2i - z(12)*2i + z(18)*2i - z(22)*2i'}
{'z(17)*2i - z(13)*2i + z(19)*2i - z(23)*2i'}
{'z(18)*2i - z(14)*2i + z(20)*2i - z(24)*2i'}
{'z(19)*2i - z(15)*2i - z(25)*2i' }
{'z(22)*2i - z(16)*2i' }
{'z(21)*2i - z(17)*2i + z(23)*2i' }
{'z(22)*2i - z(18)*2i + z(24)*2i' }
{'z(23)*2i - z(19)*2i + z(25)*2i' }
{'z(24)*2i - z(20)*2i' }
I have tried:
New = cell2mat(gg);
But I get the next error:
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
Also I have tried:
New = str2double(gg);
But I get:
Nan
I would really appreciate your help. Thank you in advance.
  댓글 수: 4
Star Strider
Star Strider 2019년 1월 5일
In your Symbolic Math Toolbox calculations, use the vpa function to resolve the problem of whatever ‘gg’ is, for esample:
gg_v = vpa(gg)
If you want to produce a function that the MATLAB numerical ODE solvers can use as an ODE function argument, see the documentation on the odeToVectorField and matlabFunction functions.
Luis Morales
Luis Morales 2019년 1월 5일
Thank you Star, I will check it.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 1월 5일
In modern MATLAB (R2017b or later),
altered_gg = regexprep(gg, '\((\d+)\)', '$1');
as_sym = str2sym(altered_gg);
z_vars = symvar(as_sym);
GG = matlabFunction(as_sym, 'Vars', {z_vars(:)});
then
[T1, Z1] = ode45(GG, time, ini);
  댓글 수: 1
Luis Morales
Luis Morales 2019년 1월 5일
Hi Walter Roberson, thank you for your reply. Doing this I got:
Error: Too many input arguments.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Do I have to do something else?
Thank you again!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by