Help with matrix dimensions error please
Info
This question is closed. Reopen it to edit or answer.
Show older comments
The following is the red text error:
Matrix dimensions must agree.
Error in chksubs (line 6)
d=(cmat-crom);
Error in update (line 3)
[s1,s2]=chksubs(cr,crom,s);
Error in gaplsopt (line 198)
[crom,resp,comp,numvar]=update(cr,crom,s(i,:),resp,comp,numvar,risp,fac,var);
This is the chksubs code:
% function chksubs cheks subsets
function [a,b]=chksubs(cr,crom,s)
a=[];
b=[];
cmat=ones(cr,1)*s;
d=(cmat-crom);
cmat2=(d==1);
v2=sum(cmat2');
a=find(v2==0); % a contains the list of the chromosomes of which s is a subset
cmat2=(d==-1);
v2=sum(cmat2');
b=find(v2==0); % b contains the list of the chromosomes that are a subset of s
and this is the update code:
% function update updates the population
function [crom,resp,comp,numvar]=update(cr,crom,s,resp,comp,numvar,risp,fac,var)
[s1,s2]=chksubs(cr,crom,s);
if isempty(s2)
mm=0;
else
mm=max(resp(s2));
end
if risp>mm % the new chrom. survives only if better
resp=[resp;risp];
comp=[comp;fac];
crom=[crom;s];
numvar=[numvar;size(var,2)];
for kk=1:size(s1,2)
if risp>=resp(s1(kk))
resp(s1(kk))=0; % the old chrom. are killed if worse
end
end
[vv,pp]=sort(resp);
pp=flipud(pp);
crom=crom(pp,:);
resp=resp(pp,:);
comp=comp(pp,:);
numvar=numvar(pp,:);
pr=zeros(cr+1,1); %%%pr stores the index of the prot. chrom. %%%
for ipr=1:max(numvar)
prot=find(numvar<=ipr&numvar>0);
if isempty(prot)==0
pr(prot(1))=1;
end
end
prot=find(pr==0);
el=max(prot); %%%el is the chrom. to be eliminated %%%
crom(el,:)=[];
resp(el,:)=[];
comp(el,:)=[];
numvar(el,:)=[];
end
8 Comments
Star Strider
on 9 Oct 2018
Jennifer —
My apologies for being a bit critical.
However yours is likely the least informative post I’ve seen here in a while. Please post the relevant parts of ‘chksubs’ and ‘update’ so we can see them, and determine what the problem might be.
Also, please copy all the red text of the error message from your Command Window, and paste it to a Comment here.
Image Analyst
on 9 Oct 2018
Since you're not posting your code, this link is your definite route to a solution: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
Jennifer Pyles
on 9 Oct 2018
Torsten
on 9 Oct 2018
After the line
cmat=ones(cr,1)*s;
insert
size(cmat)
size(crom)
and see if they are equal.
Jennifer Pyles
on 9 Oct 2018
Just run this code together with your call to chksubs:
function [a,b]=chksubs(cr,crom,s)
a=[];
b=[];
cmat=ones(cr,1)*s;
size(cmat)
size(crom)
end
If the numbers displayed (size(cmat) and size(crom)) are not equal, you can't subtract them as you do in the next line of your code.
Jennifer Pyles
on 9 Oct 2018
Jan
on 9 Oct 2018
@Jennifer: These commands do not solve the problem, but display the sizes. Then you do not have to believe, but you can see in the output, if the sizes match.
Answers (0)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!