MATLAB: linear combination of matrices

1.1k Views Asked by At

I have $N$ matrices of the same dimensions $m$-by-$n$ which span some matrix-space. E.g. the canonical basis $E_{ij}$ where the $(i,j)$-th entry is one and all others are zero.

Now would like to store them in one quantity, say a $m$-by-$n$-by-$N$ matrix or a $N$-cell, in order to quickly get linear combinations of them by prescribing a coefficient vector and performing some sort of "matrix-vector-product", circumventing looping.

Is there an elegant way to do this?

PS: I would appreciate not to embed them as vectors in $R^{mn}$ because I want to use the result in a function that only accepts matrices, so the transformation would need to be inverted.

PPS: I actually use a more interesting basis than the above. The entries of the resulting linear combination are not immediately obvious from the coefficient vector.

1

There are 1 best solutions below

0
On

A simple way is to use reshape. We start by creating a multidimensional array for some grid:

[X,Y,Z] = meshgrid(1:3,1:2,1:3);

Now Z will be three 2x3 matrices, filled with 1s 2s and 3s. The size will be in this order:

[rows, columns, number of matrices]:

[s1,s2,s3]= size(Z);

Say we want to sum all three of them we can just do:

reshape(reshape(Z,[s1*s2,s3])*[1,1,1]',[s1,s2,1])

The vector [1,1,1]' contain the coefficients of the linear combination.