Convert syms matrix to double

How to convert the matrix with syms in double.
The use of double print error.

답변 (1개)

Star Strider
Star Strider 2022년 11월 21일

0 개 추천

That is not possible. The reason is that the matrix contains symbolic variables. They have to be assigned numeric values to use the double function.
One possible approach is to use the matlabFunction funciton to turn it into a funiion —
syms a1 a2 a3
c = [0 0 0 0 0; 0 a1 0 0 0; 0 0 a2 0 0; 0 0 0 0 0; 0 0 0 0 a3];
cfcn = matlabFunction(c)
cfcn = function_handle with value:
@(a1,a2,a3)reshape([0.0,0.0,0.0,0.0,0.0,0.0,a1,0.0,0.0,0.0,0.0,0.0,a2,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,a3],[5,5])
c = cfcn(21,42,84)
c = 5×5
0 0 0 0 0 0 21 0 0 0 0 0 42 0 0 0 0 0 0 0 0 0 0 0 84
That ‘c’ matrix you can now use in other calculations outside the Symbolic Math Toolbox.
.

댓글 수: 5

Marianna
Marianna 2022년 11월 21일
I have to create the matrix c dynamic, because i have large dimension.
Torsten
Torsten 2022년 11월 21일
편집: Torsten 2022년 11월 21일
The matlabFunction is one possibility. The other is
syms a1 a2 a3
c = [0 0 0 0 0; 0 a1 0 0 0; 0 0 a2 0 0; 0 0 0 0 0; 0 0 0 0 a3];
cnum = double(subs(c,[a1 a2 a3],[21 42 84]))
cnum = 5×5
0 0 0 0 0 0 21 0 0 0 0 0 42 0 0 0 0 0 0 0 0 0 0 0 84
class(cnum)
ans = 'double'
Marianna
Marianna 2022년 11월 21일
The problem is to create the c matrix dynamic, not manualy
Torsten
Torsten 2022년 11월 21일
편집: Torsten 2022년 11월 21일
At the risk of outing me as an ignorant: What exactly do you mean by "dynamic" ? Dynamic in size ?
@Marianna — I have no idea what your requirements are for ‘c’, or how you create it.
Another option is to use the diag function. You have to determine what elements of the vector are non-zero, and either set the length using the zeros function and then fill the appropriate positions, or always have the (n,n) entry as the last element in the vector, where the dimensions of the matrix are (n,n).
Example —
c = diag([0 21 42 0 84])
c = 5×5
0 0 0 0 0 0 21 0 0 0 0 0 42 0 0 0 0 0 0 0 0 0 0 0 84
It might be possible to create that as a function as well, specifying the positions and their values as arguments, depending on what you want. (I have no idea that that is just now.)
.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2022년 11월 21일

편집:

2022년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by