How to multiply matrix of nxm with matrix nxmxp different dimensions in matlab

357 views Asked by At

In my current analysis, I am trying to multiply a matrix (flm), of dimension nxm, with the inverse of a matrix nxmxp, and then use this result to multiply it by the inverse of the matrix (flm).

I was trying using the following code:

flm = repmat(Data.fm.flm(chan,:),[1 1 morder]); %chan -> is a vector 1by3
A = (flm(:,:,:)/A_inv(:,:,:))/flm(:,:,:);

However. due to the problem of dimensions, I am getting the following error message: Error using ==> mrdivide Inputs must be 2-D, or at least one input must be scalar. To compute elementwise RDIVIDE, use RDIVIDE (./) instead.

I have no idea on how to proceed without using a for loop, so anyone as any suggestion?

1

There are 1 answers

4
Dennis Jaheruddin On

I think you are looking for a way to conveniently multiply matrices when one is of higher dimensionality than the other. In that case you can use bxsfun to automatically 'expand' the smaller matrix.

x  = rand(3,4);
y = rand(3,4,5);
bsxfun(@times,x,y)

It is quite simple, and very efficient.

Make sure to check out doc bsxfun for more examples.