Multiplication of cell arrays
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Jan
am 17 Jan. 2018
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})
Weitere Antworten (1)
Walter Roberson
am 17 Jan. 2018
Remove the loop.
mul = cellfun(@(x,y,z,s) x.*y.*z.*s, a, b, c, d, 'uniform', 0);
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!