How can I multiply cell arrays

7 views (last 30 days)
gsourop
gsourop on 19 Nov 2016
Commented: gsourop on 19 Nov 2016
Hi everyone,
I have a cell array A 2x100. Each element of A is a 6x1 matrix. If B is a 101x6 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 2x100 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.

Accepted Answer

michio
michio on 19 Nov 2016
Edited: michio on 19 Nov 2016
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

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!