Help with matrix dimensions error please

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 commentaires

Star Strider
Star Strider le 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
Image Analyst le 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
Jennifer Pyles le 9 Oct 2018
Sorry for the short question I have updated it to include both of the codes and the complete red text. Thanks!
After the line
cmat=ones(cr,1)*s;
insert
size(cmat)
size(crom)
and see if they are equal.
Jennifer Pyles
Jennifer Pyles le 9 Oct 2018
I tried what you suggested and it still came up with the same errors. I do not believe that they are equal though.
Torsten
Torsten le 9 Oct 2018
Modifié(e) : Torsten le 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
Jennifer Pyles le 9 Oct 2018
I fixed it thank you for your help.
Jan
Jan le 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.

Réponses (0)

Cette question est clôturée.

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by