MATLAB: Index in position 2 exceeds array bounds (must not exceed 1).

arrayMATLAB

ele_deof = [1 2 3 4; 1 2 5 6; 3 4 5 6; 3 4 7 8; 1 2 7 8; 5 6 7 8; 5 6 11 12; 7 8 9 10; 7 8 11 12; 5 6 9 10;9 10 11 12];
k = [2 4 -2 -4;4 7000000 -4 -7000000;-2 -4 2 4;-4 -7000000 4 7000000];
for e=1:11
% extract the rows of ele_dof (for each element e)
ele_dof_vec=ele_dof(e,:);
for i=1:4
for j=1:4
stiffness(ele_dof_vec(1,i),ele_dof_vec(1,j))=...
stiffness(ele_dof_vec(1,i),ele_dof_vec(1,j))+k(i,j);
end
end
end
I've been trying to figure this out, but still can't understand why there's an error in the stiffness(ele_dof_vec(1,i),ele_dof_vec(1,j))+k(i,j); line. Could anyone give me some hints about this? Thanks!

Best Answer

ele_dof = [1 2 3 4; 1 2 5 6; 3 4 5 6; 3 4 7 8; 1 2 7 8; 5 6 7 8; 5 6 11 12; 7 8 9 10; 7 8 11 12; 5 6 9 10;9 10 11 12];
k = [2 4 -2 -4;4 7000000 -4 -7000000;-2 -4 2 4;-4 -7000000 4 7000000];
mx = max(ele_dof(:));
stiffness = zeros(mx, mx);