How can I convert a cell array to complex double?
조회 수: 6 (최근 30일)
이전 댓글 표시
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
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.
채택된 답변
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);
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!