Symbolic vector/matrix problem

2 views (last 30 days)
hyqneuron
hyqneuron on 10 Apr 2014
Hi everyone,
I cannot figure out a way to define symbolic matrices/vectors in the way I want. I know I can do something like
sym('A', [M N])
But the above solution has several problems:
  1. A has to be of fixed size, but I don't want to specify anything other than A is a matrix
  2. When I make a function involving A using matlabFunction, I have to supply each and every element of A as separate function parameters. This is very troublesome when A is large. Also, code below doesn't work:
A = sym('A', [2 2])
B = sym('B', [2 2])
C(A,B) = A*B
In short, I want to define a symbol for a matrix, not a matrix of symbols. How can I do this?
In Theano, a python symbolic package, there are scalar symbols, vector symbols and matrix symbols, so things work in exactly the way I expect. I wonder how to do that in Mablab?

Answers (1)

Christopher Creutzig
Christopher Creutzig on 21 May 2014
matlabFunction happily handles matrices of parameters, with the meaning that the resulting function is called with a matrix in that place, but for reasons of MATLAB syntax, that requires you hand it a cell array. In the following, I've abbreviated the outputs for readability:
>> A = sym('A',[4,4]);
>> B = sym('B',[4,3]);
>> A*B
ans =
[ A1_1*B1_1 + A1_2*B2_1 + A1_3*B3_1 + A1_4*B4_1, ...
[ A2_1*B1_1 + A2_2*B2_1 + A2_3*B3_1 + A2_4*B4_1, ...
[ A3_1*B1_1 + A3_2*B2_1 + A3_3*B3_1 + A3_4*B4_1, ...
[ A4_1*B1_1 + A4_2*B2_1 + A4_3*B3_1 + A4_4*B4_1, ...
>> matmult = matlabFunction(A*B, 'Vars', {A, B})
matmult =
@(in1,in2)reshape([in1(1).*in2(1)+in1(5).*in2(2)+in1(9).*in2(3)+...
>> matmult(magic(4), reshape(1:12,[4,3]))
ans =
81 217 353
89 225 361
89 225 361
81 217 353
There is currently no way to say that A is supposed to denote an abstract matrix.

Community Treasure Hunt

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

Start Hunting!