How can I multiply cell arrays

조회 수: 1 (최근 30일)
gsourop
gsourop 2016년 11월 19일
댓글: gsourop 2016년 11월 19일
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.

채택된 답변

michio
michio 2016년 11월 19일
편집: michio 2016년 11월 19일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by