How can I multiply cell arrays
3 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
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 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!