Index in position 1 exceeds array bounds (must not exceed 1)?

30 visualizaciones (últimos 30 días)
I'm trying to run this function (Y is a 3 by 4 matrix)
function Y10 = RREF1 (Y)
Y1=Y(2,:)-(Y(2,1)/Y(1,1))*Y(1,:);
Y2=Y(3,:)-(Y(3,1)/Y(1,1))*Y(1,:);
Y3=Y2-(Y2(1,2)/Y1(1,2))*Y1;
Y4=Y3/Y3(1,3);
Y5=Y2-(Y2(1,3)/Y4(1,3))*Y4;
Y6=Y5/Y5(1,2);
Y7=Y(1,:)-(Y(1,3)/Y4(1,3))*Y4;
Y8=Y7-(Y7(1,2)/Y6(1,2))*Y6;
Y9=Y8/Y8(1,1);
Y10 = [Y9;Y6;Y4];
end
but I get this error each time i run RREF1 Y:
Index in position 1 exceeds array bounds. Index must not exceed 1.
Error in RREF1 (line 2)
Y1=Y(2,:)-(Y(2,1)/Y(1,1))*Y(1,:);
I don't understand which array is it talking about? I know Y does have more than one row or column

Respuesta aceptada

Star Strider
Star Strider el 19 de Nov. de 2021
There must be something interfering with the function, since the code runs correctly here —
Y=[1 -3 5 -9; 2 -1 -3 19;3 1 4 -13]
Y = 3×4
1 -3 5 -9 2 -1 -3 19 3 1 4 -13
Yout = RREF1(Y)
Yout = 3×4
1 0 0 2 0 1 0 -3 0 0 1 -4
function Y10 = RREF1 (Y)
Y1=Y(2,:)-(Y(2,1)/Y(1,1))*Y(1,:);
Y2=Y(3,:)-(Y(3,1)/Y(1,1))*Y(1,:);
Y3=Y2-(Y2(1,2)/Y1(1,2))*Y1;
Y4=Y3/Y3(1,3);
Y5=Y2-(Y2(1,3)/Y4(1,3))*Y4;
Y6=Y5/Y5(1,2);
Y7=Y(1,:)-(Y(1,3)/Y4(1,3))*Y4;
Y8=Y7-(Y7(1,2)/Y6(1,2))*Y6;
Y9=Y8/Y8(1,1);
Y10 = [Y9;Y6;Y4];
end
See if there is another ‘RREF1’ function somewhere that could be running instead of this one.
.

Más respuestas (1)

Cris LaPierre
Cris LaPierre el 19 de Nov. de 2021
Apparently there is only one row in Y, and your code is trying to access a second row.
Y = 1:3
Y = 1×3
1 2 3
% Works
Y (1,1)
ans = 1
% Doesn't Work
Y(2,1)
Index in position 1 exceeds array bounds. Index must not exceed 1.
  3 comentarios
ba sa
ba sa el 19 de Nov. de 2021
when I run the code in the command window it doesn't have a problem
Y1=Y(2,:)-(Y(2,1)/Y(1,1))*Y(1,:);
Y2=Y(3,:)-(Y(3,1)/Y(1,1))*Y(1,:);
Y3=Y2-(Y2(1,2)/Y1(1,2))*Y1;
Y4=Y3/Y3(1,3);
Y5=Y2-(Y2(1,3)/Y4(1,3))*Y4;
Y6=Y5/Y5(1,2);
Y7=Y(1,:)-(Y(1,3)/Y4(1,3))*Y4;
Y8=Y7-(Y7(1,2)/Y6(1,2))*Y6;
Y9=Y8/Y8(1,1);
Y10 = [Y9;Y6;Y4]
Y10 =
1 0 0 2
0 1 0 -3
0 0 1 -4
but the function doesn't work
Cris LaPierre
Cris LaPierre el 19 de Nov. de 2021
You may have redefined Y somewhere in your code before it gets passed into RREF1. Your function is only getting a 1xn vector for Y, and not a 3x4 matrix.
How do you call RREF1 in your code?

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by