cellfunc -multiplication of two cells

1 view (last 30 days)
RoboKid
RoboKid on 15 Nov 2013
Answered: sixwwwwww on 15 Nov 2013
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

Answers (2)

Walter Roberson
Walter Roberson on 15 Nov 2013
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 on 15 Nov 2013
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!

Community Treasure Hunt

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

Start Hunting!