why matlab not calculating all values in loop?
2 views (last 30 days)
Show older comments
summyia qamar
on 10 Feb 2017
Answered: Roger Stafford
on 10 Feb 2017
I'm running this code on matlab but in the last loop matlab is just displaying answers of last column and not all loop values.
demand=[25,25,10];
cost_per_unit_energy=[0.01;0.02];
RM1=[1 0];
RM2=[0 1;1 0];
RM3=[1 0];
RM4=[1 0;0 1];
Make_topart_power=[12 10;8 9;10 10;9 7];
S1=[2 3 0 0;2 0 2 3];
S2=[4 0 0 1;0 2 3 0];
S3=[2 0 2 0;3 2 0 0];
E= [S1;S2;S3];
Ez = [size(S1,1) size(S2,1) size(S3,1)];
Ec = [0 cumsum(Ez(1:end-1))];
Ea = allcomb(1:Ez(1),1:Ez(2),1:Ez(3));
En = size(Ea,1);
R = cell(1,En);
for r=1:En
R{r} =E(Ec+Ea(r,:),:);
end
RM= [RM1;RM2;RM3;RM4];
RMz = [size(RM1,1) size(RM2,1) size(RM3,1) size(RM4,1)];
RMc = [0 cumsum(RMz(1:end-1))];
RMa = allcomb(1:RMz(1),1:RMz(2),1:RMz(3),1:RMz(4));
RMn = size(RMa,1);
S = cell(1,RMn);
for s=1:RMn
S{s} =RM(RMc+RMa(s,:),:);
end
for s=1:numel(S)
power_consumption{s}=bsxfun(@times,S{s},Make_topart_power);
end
for r=1:numel(R)
for s=numel(power_consumption)
Energy_consumed=bsxfun(@times,(R{r}*power_consumption{s}),demand');
cost=bsxfun(@times,Energy_consumed',cost_per_unit_energy);
energy_cost(r,s)=sum(sum(cost));
end
end
Output=
energy_cost(r,s)=
[0 0 0 31.9000000000000
0 0 0 32.7000000000000
0 0 0 27.9000000000000
0 0 0 28.7000000000000
0 0 0 41.4000000000000
0 0 0 42.2000000000000
0 0 0 37.4000000000000
0 0 0 38.2000000000000]
somebody please help me I've tried one by one value checking using debug but it changes value for zero elements sometimes. what should I do?
0 Comments
Accepted Answer
Roger Stafford
on 10 Feb 2017
In the line “for s=numel(power_consumption)” you should have written:
for s=1:numel(power_consumption)
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!