How to add matrix in a matrix which is obtained using syms function?

B1 =
[ 0, conj(eta)/24 - 1/24, (3*conj(xi))/160 - 3/160]
[ 0, conj(eta)/80 - 1/80, conj(xi)/16 - 1/16]
[ 0, (7*conj(xi))/320 - 7/320, (7*conj(eta))/480 - 7/480]
B2 =
[ 0, 1/24 - conj(eta)/24, - (3*conj(xi))/160 - 3/160]
[ 0, 1/80 - conj(eta)/80, - conj(xi)/16 - 1/16]
[ 0, - (7*conj(xi))/320 - 7/320, 7/480 - (7*conj(eta))/480]
These are the value of B1 and B2 which are obtained using syms function. I have to add matrix B1 and B2 in a 6x6 matrix named B. How can I do this. I tried the following code but it doesn't work.
B=zeros(6);
B([1:3],[1:3])=B1;
B([4:6],[4:6])=B2;
With this code, I obtain the following error.
The following error occurred converting from sym to double: DOUBLE cannot convert the input expression into a double array.
Please help me to combine the following matrix as required.

 Accepted Answer

B=sym(zeros(6));

2 Comments

Yes, that will work on all versions of MATLAB that have supported the Symbolic Mathworks toolbox, including the original Maple based Symbolic Toolbox.
Also in sufficiently new versions of MATLAB you can
B = zeros(6,'sym');
I would need to research which version that was supported from. I think it was R2011b, but I am not certain.

Sign in to comment.

More Answers (1)

blkdiag() seems to work
blkdiag(B1,B2)
though interestingly enough it appears that blkdiag is not a supported function for the Symbolic Math Toolbox

4 Comments

If a function only uses operations and functions that are supported for a data type, those functions can work for that data type even if there is no specific implementation of that function for that type.
For the case of blkdiag, the code path in blkdiag.m that sym arrays follow require size, zeros, and concatenation to be defined for sym in order to work. Since the sym type does define those functions / operations the general implementation of blkdiag.m can work on sym arrays.
Thank you for that clarification.
Is there a way to determine if a function will work for a sym object (or any other object, come to think of it), other than code inspection or just trying it?
Not really. You can use methods() on a sym object, but that is only for specific implementations. You can
which blkdiag(sym('x'))
But arguably that is equivalent to just trying it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!