Multiplication of cell arrays

조회 수: 34 (최근 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

댓글을 달려면 로그인하십시오.

카테고리

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