MATLAB: How to multiply cell arrays

MATLABmultuply cell arrays

Hi everyone,
I have a cell array A 2×100. Each element of A is a 6×1 matrix. If B is a 101×6 matrix. I want to multiply each raw of the last 100 raws of B with every element of A. Thus at the end it should deliver a 2×100 cell array of scalars. I 've tried
for k=1:2;
for i=1:100;
C=num2cell(cell2mat(A(k,:)).*cell2mat(B{k,2:end}(:,1)));
end;
end;
Unfortunately it doesn't work. Thanks in advance.

Best Answer

Not sure if the way you described is the best way to proceed but here you can make use of cellfun to achieve I think what you described.
A = rand(12,100);
Ac = mat2cell(A,[6,6],ones(1,100));
B = rand(101,6);
Bc = mat2cell(B(2:end,:),ones(100,1),6);
Created sample data. Ac is a 2x100 cell array with 6x1 vector in each cell. Bc is a 100x1 cell array with 1x6 vector in each cell. Now cellfun to multiply vectors in each cells
BcAc1 = cellfun(@mtimes,Bc',Ac(1,:), 'UniformOutput', false);
BcAc2 = cellfun(@mtimes,Bc',Ac(2,:), 'UniformOutput', false);
% Concatinate the results to create 2x100 cell array with a scalar in each cell
BcAc = [BcAc1; BcAc2];
whos BcAc