Kashif
Kashif

Reputation: 3327

how to take the product of all the cells in a cell array in matlab

how can I take the product of all the cells of a cell array in Matlab? In my case, I have a cell array try_this with 125 cells. Each cell is a 3x3 matrix.

I would like to take the product over all of these matrices.

Is there a good way to do this?

Upvotes: 1

Views: 251

Answers (1)

mikkola
mikkola

Reputation: 3476

It is possible to do this using a for-loop. The following collects the output piece-by-piece to the array result:

result = try_this{1}*try_this{2}; %// multiply first two cells
for k = 3:numel(try_this)
    result = result * try_this{k}; %// C{1}*C{2}* ... * C{k}
end

Edit: As discussed in the comments below, vectorizing such repeated matrix multiplication is not straightforward.

Upvotes: 2

Related Questions