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
0 个评论
采纳的回答
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
2018-1-17
Remove the loop.
mul = cellfun(@(x,y,z,s) x.*y.*z.*s, a, b, c, d, 'uniform', 0);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!