Ruud H.G. van Tol on Thu, 21 Mar 2024 19:06:52 +0100 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: Error in pari/gp code |
On 2024-03-21 17:10, Swati, NoFirstName wrote:
I am trying to write a code to output the position of the last non-zero entry of this matrix but the code is giving a syntax error. I am not sure what went wrong. Could someone help in this regard?Thanks, Swati C = [[1, 2, 3, 4], [7, 9, 10], [25, 31, 0], [0, 0, 0]]; A = matconcat([C[1]; C[2]; C[3]; C[4]]); R = 0; C = 0; S= 0; for(i = matsize(A)[1], i >= 1 && S == 0, i--, for(j = matsize(A)[2], j >= 1 && S== 0, j--, if(A[i, j] != 0, R = i; C = j; S = 1; break; ))) if(S == 1, print("Position of the last non-zero entry:" [R, C]), print("Matrix A contains only zeros.") );
I didn't get why C[1] has 4 elements, so I made it: ? doit(D=0) = { my( a = [ [1,2,3], [7,9,10], [25,31,0], [0,0,0] ], r=0, c=0 ); a = Mat(a~); D && printp(a); D && print(matsize(a)); for( i=1, matsize(a)[1] , for( j=1, matsize(a)[2] , if( a[i,j], [r,c] = [i,j] ) ) ); if( r , print("Position of the last non-zero entry:",[r,c]) , print("Matrix contains only zeros.") ); } ? doit(1) [ 1 2 3] [ 7 9 10] [25 31 0] [ 0 0 0] [4, 3] Position of the last non-zero entry:[3, 2] -- Ruud