Multiplication of cell arrays

26 ビュー (過去 30 日間)
lucksBi
lucksBi 2018 年 1 月 17 日
コメント済み: lucksBi 2018 年 1 月 17 日
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 件のコメント
lucksBi
lucksBi 2018 年 1 月 17 日
yea dbstop helped. Thank You

サインインしてコメントする。

その他の回答 (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

サインインしてコメントする。

カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by