Multiplication of cell arrays

37 次查看(过去 30 天)
hey
i have following four cell arrays(2x7 cell). i want to multiply all four. Like cell1 of a with cell 1 of b,c,d and same for all cells.
a = {[],[0.13,0.13],1.13,[0.13,0.1,1.13],[0.13,1.1,0.13],[],[]; [],[],[-1.72,-0.72,1.2],0.27,[],[0.27,1.2],[-1.7,0.2,-0.7,1.28]}
b= {[],[-0.4,-0.4],-0.2,[-0.4,-0.4,1.8],[-1.28,-1.2,0.78],[],[]; [],[],[-2.2,-1.2,0.7],0.5,[],[-0.09,-1.0],[-0.31,0.6,-2.3,1.6]}
c= {[],[1,1],1,[1,1,1],[1,1,1],[],[]; [],[],[1,1,1],1,[],[1,1],[1,1,1,1]}
d= {[],[1,1],1,[1,1,1],[1,1,1],[],[]; [],[],[1,1,1],1,[],[1,1],[1,1,1,1]}
I have used:
for x=1:2
for y=1:7
mul{x,y}=cellfun(@(x,y,z,s) x.*y.*z.*s, a{x,y},b{x,y},c{x,y},d{x,y},'UniformOutput', false);
end
end
which gives following error:
Input #2 expected to be a cell array, was double instead.
Also these arrays doesn't only contain two rows.
kindly help

采纳的回答

Jan
Jan 2018-1-17
cellfun is smart, but harder to debug than a simple loop.
mul = cell(2, 7);
for x=1:2
for y=1:7
mul{x,y} = a{x,y} .* b{x,y} .* c{x,y} .* d{x,y};
end
end
Does this work? I assume if fails with the same error. Then use the debugger:
dbstop if error
Run the code again until it stops at the error. Now check the sizes:
size(a{x,y})
size(b{x,y})
size(c{x,y})
size(d{x,y})

更多回答(1 个)

Walter Roberson
Walter Roberson 2018-1-17
Remove the loop.
mul = cellfun(@(x,y,z,s) x.*y.*z.*s, a, b, c, d, 'uniform', 0);
  1 个评论
lucksBi
lucksBi 2018-1-17
ThankYou. It works fine of two rows but on matrix of 8x7 it gives following error:
All of the input arguments must be of the same size and shape. Previous inputs had size 1 in dimension 1. Input #3 has size 8

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by