Error using Reshape pls help

1 view (last 30 days)
Bhavz
Bhavz on 1 Nov 2014
Commented: Bhavz on 1 Nov 2014
My code is as follows and the error i get is
Error using reshape To RESHAPE the number of elements must not change.
Error in maint (line 148) l_dp=reshape(l_dp1,4,8);
Can someone pls help
code:-
% delta_edp : if link e on path p used for demand d
% x_dp : if path used for demand
% y_e : if link e used in any primary path
% l_dp : the fraction of traffic via path p
%link
global hd ce Ye delta_edp X_dp ;
link(1,:)=[1 2];
link(2,:)=[1 3];
link(3,:)=[1 4];
link(4,:)=[2 6];
link(5,:)=[3 6];
link(6,:)=[3 7];
link(7,:)=[4 8];
link(8,:)=[4 9];
link(9,:)=[6 10];
link(10,:)=[7 10];
link(11,:)=[8 11];
link(12,:)=[8 12];
link(13,:)=[9 12];
link(14,:)=[9 13];
link(15,:)=[1 5];
link(16,:)=[5 13];
%primary path
Pd_pr(1,:)=[2, 6, 10];
Pd_pr(2,:)=[3,7,11];
Pd_pr(3,:)=[3,7,12];
Pd_pr(4,:)=[15, 16,0];
%sencondary path
Pd_s(1,:)=[1,4,9];
Pd_s(2,:)=[2,5,9];
Pd_s(3,:)=[3,8,13];
Pd_s(4,:)=[3,8,14];
path=[Pd_pr;Pd_s];
%link capacity
ce=[50 50 200 50 50 50 150 50 50 50 50 50 50 50 50 50];
%demand
demand=[1 10;1 11;1 12;1 13];
%associated flow values for demand
hd=[50 50 50 50];
%value of X_dp
X_dp=zeros(4,8);
for jj=1:4
for kk=1:8
demandStart=demand(jj,1); %start node of demand
demandEnd=demand(jj,2); %end node of demand
pathStart=path(kk,1); %start node of path
linkStart=link(pathStart,1);%start node of link
pathEnd=path(kk,3); %end node of path
if pathEnd==0
pathEnd=path(kk,2);
end
linkEnd=link(pathEnd,2); %end node of link
if pathEnd==0
pathEnd=path(kk,2);
end
if demandStart==linkStart && demandEnd==linkEnd
X_dp(jj,kk)=1;
end
end
end
X_dp1=reshape(X_dp,1,[]);
%value of delta_edp
delta_edp=zeros(16,4,8);
for ii=1 : 16 % link
for jj=1: 4 % demand
for kk=1:8 % path
for nn=1:3 %link of path
if path(kk,nn)==ii && X_dp(jj,kk)==1
delta_edp(ii,jj,kk)=1;
end
end
end
end
end
%value of Ye
Ye=zeros(16,1);
for ii=1:16 %link
for kk=1:4 %demand
for nn=1:3 %link of path
if Pd_pr(kk,nn)==ii
Ye(ii)=1;
end
end
end
end
lb = zeros(1,32); ub=X_dp1;
%fh = double (f) ;
%options = optimoptions('intlinprog','Display','iter'); %intcon=1:32; %[x,fval,exitflag,output]=intlinprog(f,... % 32,intcon,[],[],[],lb,ub,options);
options = gaoptimset('Generations',50,'PopulationSize',42,... 'PlotFcns',@gaplotbestf); nonlcon=@constraint; [x,fval,exitflag] = ga(@objectiveFun,... 32,[],[],[],[],lb,ub,nonlcon,options);
l_dp=reshape(x,4,8)
min_romax=fval;

Accepted Answer

Chad Greene
Chad Greene on 1 Nov 2014
How many elements are in l_dp before you try to reshape it? To reshape it into a 4 x 8 arrangement, numel(l_dp) must equal 32. Here's an example
a = magic(6)
Above, a is 6-by-6 and thus has as 36 elements. You can reshape a into a 4-by-9 matrix by
a = reshape(a,4,9)
because 4 times 9 is 36, meaning there's you're not trying to change the number of elements in a when you reshape it. If you try to reshape a into a 4-by-8 matrix, it'll give you an error:
reshape(a,4,8)
Error using reshape
To RESHAPE the number of elements must not change.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!