cellfunc -multiplication of two cells

1 次查看(过去 30 天)
RoboKid
RoboKid 2013-11-15
回答: sixwwwwww 2013-11-15
how to multiply two cells let's say:D = {[1 2 ] , [1 3] ; [1 6] , [5 2] } L={[1],[0],[0],[0];[0],[1],[0],[0]}
I want to do L*D how do I do that? --Thanks

回答(2 个)

Walter Roberson
Walter Roberson 2013-11-15
What answer are you expecting?
L is 2 x 4 with each element 1 x 1, so most likely L should be considered to be like a 2 x 4 matrix.
D is 2 x 2 with each element 1 x 2. One of the ways to view that would be as a 2 x 4 matrix.
But if one views this as a (2 x 4) * (2 x 4) then the "*" operator must fail because the inner dimensions do not agree.
I am going to speculate that you asked the wrong question and that what you want is L .* D
cell2mat(L) .* cell2mat(D)

sixwwwwww
sixwwwwww 2013-11-15
Dear Mihnathul, I completely agree with Walter. Your question is not completely clear, however if you really insist to use cellfun for this purpose then maybe you can try something like this:
if prod(size(L)) >= prod(size(D))
Mat = cellfun(@times, num2cell(reshape(cell2mat(D), size(L, 1), size(L, 2))), L);
else
Mat = cellfun(@times, D, num2cell(reshape(cell2mat(L), size(D, 1), size(D, 2))));
end
disp(Mat)
The code is nothing in itself. Just it is making dimensions of 2 cell arrays equal so that you can use cellfun. I hope it helps. Good luck!

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by