Showing results for 
Search instead for 
Did you mean: 

Matrix operations in SystemBuild

Matrix operations in SystemBuild


I'm a beginner trying to implement a Model Predictive Controller (MPC) controller in SystemBuild for my final thesis project. This involves a bunch of matrix operations being performed and some resulting matrices being sent to a UserCode Fortran script, solving a Quadratic Programming problem. At the moment I have two specific, and I hope quite simple, questions:


  • How do I sum two matrices together? There appears to be no block for this and I only get the 'Summer Block' to sum together scalars.
  • Is there an efficient way to import matrix data from a Matlab environment? At present I have defined constant blocks representing matrices but since UNIX doesn't allow copy-paste of parameter values directly into the parameter setting box I would have to manually type in huge amounts of data, can I get around this?

It should be noted that the solutions must work with the AutoCode feature.


Any advice or source of information is appreciated, I find the SystemBuild User Guide a bit thin.


Best regards,


0 Kudos
Message 1 of 3

Re: Matrix operations in SystemBuild

The SystemBuild summer block is an element-by-element summer.

To add two matricies, set the number of branches to 2 (the default) and set the number of outputs to match the number of matrix elements.

To make it easier to connect matricies between SystemBuild blocks, You should set the block InputPins and OutputPins modes to vector and set inputnames and outputlabels as vectors labels.

Here is an SBA script example you can cut and paste into the Xmath command line which adds two constant blocks (matrix) using a summer.



createsuperblock "matrixsum",{outputs=12}

createblock "constant",{constantvalue=3*ones(3,4),constantname="mat1",id=1,outputlabel="mat1[3x4]",outputpins="Vector",location=[100,0],icontype="Alternate",showoutputlabels="on"}

createblock "constant",{constantvalue=4*ones(3,4),constantname="mat2",id=2,outputlabel="mat2[3x4]",outputpins="Vector",location=[100,-100],icontype="Alternate",showoutputlabels="on"}

createblock "summer",{signs=[1,1],outputs=12,id=3,outputlabel="sumout[3x4]",inputname="mat1[3x4]",inputname13="mat2[3x4]",inputpins="vector",outputpins="vector",location=[250,-50]}

modifyblock 3,{icontype="Alternate",color=1,showoutputlabels="on"}

createconnection 1,3

createconnection 2,3

createconnection 3,0


[,y] = sim("matrixsum",t)


Reading Matlab data into Xmath is tricky because the Matlab save format is closed and changing.

If you can write out you Matlab data in ASCII format and remove and headers or labels and are

just left with just numbers seperated by spaces. You could use the Xmath read command

to load it into Xmath as a matrix.


A = read("$XMATH/demos/read_ex.ascii",10,10,"ascii")? 


Check out Xmath Math User Guide page C-16 Loading In External Data (read)


Message 2 of 3

Re: Matrix operations in SystemBuild

Thanks a lot! That solved my problems for the time being Smiley Happy
0 Kudos
Message 3 of 3