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

28 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 19 Nov 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.
.

More Answers (1)

Cris LaPierre
Cris LaPierre on 19 Nov 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 Comments
ba sa
ba sa on 19 Nov 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 on 19 Nov 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?

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!