Asked by Thidarat
on 30 Sep 2013

l'm doing direction-8 chain code.I get this error "In an assignment A(I) = B, the number of elements in B and I must be the same. Error in IPUTT2 (line 48) tt(i+1)=BB(x+n(j,1),y+n(j,2));"

/////////////////////////////////////////////////////////////////////////////////

it is code

function IPU = IPUTT2(BrowseImage) input = colorhistf(BrowseImage); %color feature

Im=imread(BrowseImage); IG=rgb2gray(Im); B=edge(IG,'canny'); BB=uint8(B); %disp('BB'); %disp(BB); glcm0 = graycomatrix(BB,'NumLevels',2,'offset',[0 1],'G',[]); %co-occurrence 0 glcm45 = graycomatrix(BB,'NumLevels',2,'offset',[-1 1],'G',[]); %co-occurrence 45 glcm90 = graycomatrix(BB,'NumLevels',2,'offset',[-1 0],'G',[]); %co-occurrence 90 glcm135 = graycomatrix(BB,'NumLevels',2,'offset',[-1 -1],'G',[]); %co-occurrence 135 D=[reshape(glcm0,1,[]) reshape(glcm90,1,[])]; D2=[reshape(glcm45,1,[]) reshape(glcm135,1,[])]; input2=D/sum(D); input3=D2/sum(D2); %input2=D/sum(D)+D2/sum(D2);

%Direction-8 chain code n=[0 1; -1 1; -1 0; -1 -1; 0 -1; 1 -1; 1 0; 1 1]; flag=1; cc=[];

[x y]=find(BB==1);

x=min(x); imx=BB(x,:); y=min(find(imx==1)); first=[x y]; dir=7;

while flag==1, tt=zeros(1,8); newdir=mod(dir+7-mod(dir,2),8);

for i=0:7, j=mod(newdir+i,8)+1; tt(i+1)=BB(x+n(j,1),y+n(j,2)); end d=min(find(tt==1)); dir=mod(newdir+d-1,8); cc=[cc,dir]; x=x+n(dir+1,1); y=y+n(dir+1,2);

if x==first(1) & y==first(2) flag=0; end; end; out=cc; disp('out'); disp(out);

IPU = [input; input2; input3]'; end

Answer by Jan Simon
on 30 Sep 2013

Edited by Jan Simon
on 30 Sep 2013

The standard method to handle such problems is (a simple search in the forum would reveal this fast):

dbstop if error

Then run the code again until it stops at the error. Then check the sizes of the locally used variables:

size(tt(i+1)) size(BB(x+n(j,1),y+n(j,2)))

Note: Shadowing Matlab's toolbox functions by variables results in unexpected troubles frequently. Here this concerns "input", "dir", "flag". This is most likely not the problem in your code, but it could other problems elsewhere.

Log in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Jan Simon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/88641-error-in-an-assignment-a-i-b-the-number-of-elements-in-b-and-i-must-be-the-same#comment_171561

Please do not write "it is code", but simply use the standard method of formatting code in the forum. Follow the "? Help" link to learn the way to format messages here, when you want others to spend the time to read your code.

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/88641-error-in-an-assignment-a-i-b-the-number-of-elements-in-b-and-i-must-be-the-same#comment_171586

http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

Log in to comment.