cellfunc -multiplication of two cells
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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)
0 个评论
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 GPU Computing in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!