How can I convert a cell array to complex double?

6 Ansichten (letzte 30 Tage)
Luis Morales
Luis Morales am 5 Jan. 2019
Kommentiert: Luis Morales am 5 Jan. 2019
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 Kommentare
Star Strider
Star Strider am 5 Jan. 2019
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 am 5 Jan. 2019
Thank you Star, I will check it.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 5 Jan. 2019
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 Kommentar
Luis Morales
Luis Morales am 5 Jan. 2019
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by