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

조회 수: 4 (최근 30일)
summyia qamar
summyia qamar 2017년 1월 23일
답변: Bontle Lekgoate 2022년 4월 10일
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
the cyclist
the cyclist 2017년 1월 23일
I'm confused. Is A a 1x1 cell array, and that one cell contains a 1x64 double? Or is it a 1x64 cell array? Or is A actually just a 1x64 double, not really a cell array?
Similar questions for B.
Please explain in more detail, or load the data into a MAT file and upload it.
the cyclist
the cyclist 2017년 1월 23일
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.

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

답변 (3개)

Guillaume
Guillaume 2017년 1월 23일
편집: Guillaume 2017년 1월 24일
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
Andrei Bobrov 2017년 1월 23일
편집: Andrei Bobrov 2017년 1월 23일
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

Bontle Lekgoate
Bontle Lekgoate 2022년 4월 10일
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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by