MATLAB: Array index

array index

Hi, I have declared the size of array as 4*4.When i run the third for loop the size of array becomes 49*49.Dont know why this happens in that loop?
pat1='11';
arr=zeros(4,4);
len=length(pat1);
for i=1:4
for j=1:4
arr(i,j)=l+2;
end
end
for j=1:4
arr(1,j)=1;
end
for i=1:len-1
arr(pat1(i),pat1(i+1))=l-i;
end
for i=1:4
arr(i,1)=l+1;
end
[EDITED, Jan Simon, Code formatted]

Best Answer

Because "pat1" is a character array, you are accessing the numerical equivalent of the character '1', which is 49. MATLAB is implicitly using "double('1')" to convert that character to a numeric.
Not sure why you defined pat1 that way, but you could use the following in your third loop:
arr(str2num(pat1(i)),str2num(pat1(i+1)))=l-i;
(If that is indeed what you intend.)