The error "Matrix dimensions must agree"

1 view (last 30 days)
Hi all
I am getting this error
"Matrix dimensions must agree.
Error in TwoDTensorRev4 (line 106)
TbinAreaSum= double(TbinAreaSum)+double(binAreaSum);"
Here is the code and the input file is attached as well. Please suggest. Thank you.
s=load('C20-28D-150kPa-voidForFabricStudyXZ.mat');
fieldname=fieldnames(s)
m=s.Avizo_C20_28D_150kPa_voidForFabricStudyXZ_mat;
sm=squeeze(m);
sm = bwareaopen(sm,2,4);
szdim=size(sm,3);
slices=szdim;
slices=200;
pieslice=25;
indcs=zeros(size(sm,1),size(sm,2));
porientd=zeros(slices,1);
num=zeros(slices,1);
perea=zeros(slices,1) ;
avgperea=zeros(slices,1);
edges=zeros(1,pieslice,1);
sumareapd=zeros(slices,1);
TbinAreaSum=zeros(pieslice-1,1);
for jj=1:slices
[L2,num(jj,1)] = bwlabel(sm(:,:,jj));
areap=zeros(num(jj,1),1);
areapd=zeros(2.*num(jj,1),1);
areapactual=zeros(num(jj,1),1);
binAreaSum=zeros(pieslice-1,1);
perip=zeros(num(jj,1));
vector=zeros(num(jj,1),2);
vectord=zeros(2.*num(jj,1),2);
cent=zeros(num(jj,1),1);
hbin=zeros(1,2.*num(jj,1));
thetap=zeros(1,2.*num(jj,1));
sumperea(jj)=0;
for ii=1:num(jj,1)
areao=regionprops((L2==ii),'area');
perio=regionprops((L2==ii),'perimeter');
areap(ii,1)=areao.Area./(areao.Area);
areapactual(ii,1)=areao.Area;
perip(ii,1)=perio.Perimeter;
perea(ii,jj)=perip(ii,1)./areapactual(ii,1);
coeff=pca(indcs);
vector(ii,1)=coeff(2,1);
vector(ii,2)=coeff(1,1);
sumperea(jj)=sumperea(jj)+perea(ii,jj);
end
sumareapd(jj,1)=2.*sum(areap(:,1));
avgperea(jj,1)=sumperea(jj)./num(jj);
vectord=[vector;-vector];
areapd=[areap;areap];
for mm=1:2.*num(jj,1)
thetap(mm)=atan2d(vectord(mm,2),vectord(mm,1));
if (thetap(mm) < 0)
thetap(mm)=360+thetap(mm);
else
thetap(mm)=thetap(mm);
end;
end;
for nn = 1:pieslice
edges(1,nn)=(nn-1).*pi./((pieslice-1)./2.0);
end
hbin = discretize(round(deg2rad(thetap),10),round(edges,10));
binAreaSum=accumarray(hbin',areapd);
TbinAreaSum= TbinAreaSum+binAreaSum;
end
  2 Comments
KSSV
KSSV on 17 Apr 2018
Getting different error on running the code:
Undefined function or variable 'indcs'.
Error in Junk (line 38)
coeff=pca(indcs);
Ashbub
Ashbub on 17 Apr 2018
Thankyou. I have defined the indcs. Please have a look.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Apr 2018
pieslice=25;
TbinAreaSum=zeros(pieslice-1,1);

so TbinAreaSum is (25-1)=24 by 1. You then do not change the contents until the last line where you try

TbinAreaSum= TbinAreaSum+binAreaSum; 

binAreaSum was created as

binAreaSum=zeros(pieslice-1,1);

which is also 24 x 1. But you write all over it, in the second last line,

binAreaSum=accumarray(hbin',areapd); 

so binAreaSum will only be 24 x 1 if hbin' happens to have 24 unique values in it.

Now you create hbin as

hbin = discretize(round(deg2rad(thetap),10),round(edges,10));

after

for nn = 1:pieslice                       
edges(1,nn)=(nn-1).*pi./((pieslice-1)./2.0);
end

so edges has 25 entries -- but it is not immediately clear that round(edges,10) happens to have 24 unique entries. It might be 25 unique entries; it might be less.

You need to put in a breakpoint before the accumarray and check length(unique(hbin))

  1 Comment
Ashbub
Ashbub on 17 Apr 2018
Thanks a lot. You've hit the right spot. Used histcounts instead of discretize and made few changes to remove the need for using accumarray as well. It did the trick.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!