Reputation: 733
I have a vector x = [1 2 3]
and a 3 by 3 cell array C
that consists of N by N matrices. E.g. C{1,1}
is an N by N matrix.
I want to multiply x
and C
and get a cell array D
of size 1 by 3:
D{1,1} = x(1)C{1,1) + x(2)C{2,1} + x(3)C{3,1}
D{1,2} = x(1)C{1,2) + x(2)C{2,2} + x(3)C{3,2}
D{1,3} = x(1)C{1,3) + x(2)C{2,3} + x(3)C{3,3}
Upvotes: 3
Views: 174
Reputation: 30579
For a vector x
of length M=3
, and an M-by-M
cell array C
, the idea is to form an M*N-by-M
matrix, where column i
is composed of all the values in the cells C{i,:}
. Then you can use matrix multiplication with x.'
to get the values in D
, and mat2cell
to break up the result into a new cell array.
Ct = C';
Dvals = reshape([Ct{:}],[],M)*x.';
D = mat2cell(reshape(Dvals,N,[]),N,N*ones(M,1))
Test Data:
N = 4; M = 3; x = 1:M;
C = mat2cell(rand(N*M,N*M),N*ones(M,1),N*ones(M,1));
Note: This requires that each sub-matrix is the same size, as in the question.
Upvotes: 3
Reputation: 32873
D = cell(1, 3);
D{1} = x(1) * C{1,1} + x(2) * C{2,1} + x(3) * C{3,1};
D{2} = x(1) * C{1,2} + x(2) * C{2,2} + x(3) * C{3,2};
D{3} = x(1) * C{1,3} + x(2) * C{2,3} + x(3) * C{3,3};
D = cell(1, 3);
for i=1:3
D{i} = x(1) * C{1,i} + x(2) * C{2,i} + x(3) * C{3,i};
end
D = cell(1, 3);
for i=1:3
D{i} = x(1) * C{1,i};
for j=2:3
D{i} = D{i} + x(j) * C{j,i};
end
end
Upvotes: 0