# how to add a cell array in another cell array row wise?

29 views (last 30 days)
summyia qamar on 23 Jan 2017
Answered: Bontle Lekgoate on 10 Apr 2022
this is the real code.. in the last loop I get 2 cell arrays total_energy_cost(i) and total_all(i,c).I want to add total_energy_cost(i) which is 1x64 cell array and total_all(i,c) which is 64x1806 cell array in such a way all 64 elements of "total_energy_cost(i)" are added to 64 rows of total_all(i,c).
no_of_machines=7;
no_of_cells=3;
No_of_Parts = 6;
Make_topart_power=[12; 7; 4; 6; 12; 6; 8];
move_cost=[12;12;12;12;12;12];
demand=[600 550 500 520 620 500];
labor_cost=[1.04; 1.2; 1.44; 1.6];
OP1=[10 0 0 10 0 20 15;10 13 0 0 12 0 15];
OP2=[0 10 10 15 0 0 20;11 0 13 0 17 10 0];
OP3=[10 0 0 12 11 0 0;0 11 18 0 0 0 17];
OP4=[10 0 0 0 19 0 14;0 14 0 17 0 10 0];
OP5=[18 13 0 0 0 15 0;10 12 0 0 10 0 11];
OP6=[0 10 0 0 0 10 15;12 11 0 13 0 15 0];
setup_cost=[20; 30; 25; 40; 45; 30];
hold_cost=[4; 3; 5; 3; 4; 5];
batch_size=(demand'.*setup_cost.*2./hold_cost).^0.5;
no_of_batches=demand'./batch_size;
M1=[1 0 0 0;0 0 1 0];
M2=[0 0 1 0;0 0 0 1];
M3=[0 1 0 0;0 0 0 1];
M4=[1 0 0 0;0 0 1 0];
M5=[0 1 0 0;0 0 0 1];
M6=[0 0 1 0;0 0 0 1];
M7=[0 1 0 0;0 0 0 1];
E= [OP1;OP2;OP3;OP4;OP5;OP6];
Ez = [size(OP1,1) size(OP2,1) size(OP3,1) size(OP4,1) size(OP5,1) size(OP6,1)];
Ec = [0 cumsum(Ez(1:end-1))];
Ea = allcomb(1:Ez(1),1:Ez(2),1:Ez(3),1:Ez(4),1:Ez(5),1:Ez(6));
En = size(Ea,1);
all_comb_of_OP_routes = cell(1,En);
for i=1:En
all_comb_of_OP_routes{i} =E(Ec+Ea(i,:),:);
end
CELL = zeros(no_of_cells^no_of_machines,no_of_machines);
t = 0;
for k = 0:(no_of_cells^no_of_machines)-1
s = dec2base(k,no_of_cells,no_of_machines);
if length(unique(s))==no_of_cells
t = t+1;
CELL(t,:) = s-'0'+1;
end
end
CELL = CELL(1:t,:);
combination_array=num2cell(CELL,2);
for c=1:numel(combination_array )
R=1:numel(combination_array{c});
Z = zeros(R(end),max(combination_array{c}));
Z(sub2ind(size(Z),R,combination_array{c})) = 1;
allCells_array{c}=Z;
end
for i=1:numel(all_comb_of_OP_routes)
energy_consumption=(all_comb_of_OP_routes{i}*Make_topart_power).*demand';
total_energy_cost(i)=(sum(energy_consumption))*0.01;
for c=1:numel(allCells_array)
movement=(all_comb_of_OP_routes{i})*allCells_array{c};
movement=movement>0;
total_movement_cost=sum(bsxfun(@times,bsxfun(@times,sum(movement,2)-1,no_of_batches),move_cost));
total_all(i,c)=total_movement_cost;
end
end
##### 2 CommentsShowHide 1 older comment
the cyclist on 23 Jan 2017
In the code you just posted, total_energy_cost and total_all are class double, and are NOT cell arrays. It is important for you to learn the difference between these data types, or you will confuse yourself and others.

Guillaume on 23 Jan 2017
Edited: Guillaume on 24 Jan 2017
cellfun(@plus, A, B, 'UniformOutput', false)
will do it. However, I wouldn't use a cell array to store these numbers and instead convert them to matrices, after which you can use Andrei's answer:
A = cell2mat(A);
B = cell2mat(B);
A(:).*B %or bsxfun(@plus, A(:), B) for earlier than R2016b

Andrei Bobrov on 23 Jan 2017
Edited: Andrei Bobrov on 23 Jan 2017
Let
A = randi(1000,1,64);
B = randi(1000,64,1806);
C = A(:)+B; % for R2016b and later
C = bsxfun(@plus,A(:),B); % for older version
summyia qamar on 23 Jan 2017
I want to add not multiply

Bontle Lekgoate on 10 Apr 2022
Create a cell array to contain the data of the last row given below. Note that the mark values (averages) must be calculated from the data in the corresponding columns and not hard coded. Then add the cell array data as the last row in the table