Matrix of symbols?
3 views (last 30 days)
Show older comments
Matteo Sodano
on 13 Oct 2018
Commented: Walter Roberson
on 13 Oct 2018
I have an init script with some symbols declared:
syms q1 q2 q3 q4 q5 q6
This script calls a function, where I try to build a matrix that depends on those symbols:
function angular_jacobian(T,n,joint,q)
JA(:,1) = [0; 0; 1];
for i=2:n
if joint(i) == 'p'
JA(:,i) = [0; 0; 0];
else
disp(T(1:3,3,i-1));
JA(:,i) = simplify(T(1:3,3,i-1));
end
end
disp(JA);
end
where T is a vector of 4x4 matrices. I'm just trying to build a 3xN matrix, called JA, that has first column [0;0;1], and then all the others equal to a column of zeros or to T(1:3,3,i-1). The error I get is the following:
The following error occurred converting from sym to double:
Unable to convert expression into double array.
This happens only if T(1:3,3,i-1) has symbols in it (I tried with scalar T(1:3,3,i-1) and it works). Of course every matrix T(1:3,3,i-1) has only declared symbols (I built T with other functions).
Does anybody know what I am ignoring? I have actually no idea.
2 Comments
Accepted Answer
Walter Roberson
on 13 Oct 2018
JA = sym(zeros(3,n));
simplify always returns a sym even if given numeric input, so we can conclude that you always want a sym output.
1 Comment
Walter Roberson
on 13 Oct 2018
The data type for a variable is determined by what is first assigned to it. When you happen to assign numeric [0; 0; 0] first (because you did not pre-allocate the matrix) then the datatype becomes double. Numeric arrays cannot store symbolic expressions, and try to convert them to double precision numbers, but that will fail if the symbolic expressions contain unresolved symbolic variables.
So you work around the problem by initializing to symbolic in the first place.
More Answers (1)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!