MATLAB: “Index exceeds matrix dimensions.”

arraydebug4mematrixmatrizybus

clear all
clc
nodos=input('Ingresa la cantidad de nodos del sistema\n n = ');
noZ=input('Ingresa la cantidad de lineas del sistema \n NoZ = ');
for i=1:noZ %Ciclo para ingresar los valores de la Z de rama.
ne(i)=input('Número de referencia del nodo emisor = ');
nr(i)=input('Número de referencia del nodo receptor = ');
%clc
fi=2%('Número de filas de la matriz = ');
co=2%2'Número de columnas de la matriz = ');
clc
disp(['Ingresa la matriz de impedancias de la linea #',num2str(i),':'])
for j=1:fi%Filas
for jj=1:co%Columnas
disp(['El elemento (',num2str(j),',',num2str(jj),')'])
Ztramo(j,jj,i)=input('');
end
end
clc
disp(['La matriz de impedancias del tramo #',num2str(i),' es:'])
Ztramo% Muestra Z de tramo con ceros.
disp(['La matriz de admitancias del tramo #',num2str(i),' es:'])
Ytramo(:,:,i)=inv(Ztramo(:,:,i));
Ytramo
ybus=diag(0,(nodos*2)-1);
end
for i=1:noZ %Ciclo para la formación de YBUS
k1=ne(:,:,i);
k2=nr(:,:,i);
ybus(k1,k1)=ybus(k1,k1)+Ytramo(:,:,i);
ybus(k2,k2)=ybus(k2,k2)+Ytramo(:,:,i);
ybus(k1,k2)=-Ytramo(:,:,i);
ybus(k2,k1)=ybus(k1,k2);
end
clc
ybus

Best Answer

Ztramo is an fi by co by noZ matrix.
inv(Ztramo(:,:,i)) is a fi by co matrix if it works at all, which can only happen if fi and co are equal. Therefore
Ytramo(:,:,i)=inv(Ztramo(:,:,i));
makes Ytramo as a fi by co by noZ matrix.
Then in
k1=ne(i);
k2=nr(i);
k1 and k2 are scalars because i is a scalar and indexing by a scalar always gives a scalar.
ybus(k1,k1)=ybus(k1,k1)+Ytramo(:,:,i);
So the left hand size, ybus(k1,k1) is a scalar, and the right hand side of the equation needs to be a scalar. Looking at it, the ybus(k1,k1) part of the addition is a scalar, but Ytramo(:,:,i) we have determined must be fi by co . Adding a scalar to a matrix that is fi by co gives a matrix that is fi by co. Therefore the result of the addition is a matrix that is fi by co, and that is too big to store into the scalar area designated by the left side, ybus(k1,k1)