0

I am trying to find the determinant of an $N\timesN$ matrix. Here's my code:

clc
function determinant=take_detm(A)
order=sqrt(length(A))
disp(order)
if order==2 then 
    determinant=A(1,1)*A(2,2)-A(1,2)*A(2,1);

else

    s=0
    for i=1:order
        s=s+((-1)^(i+1))*A(1,i)*take_detm(A(:,i)=[]);//deleting 1st row and a column in the recursive call
    end
    determinant=s

end
endfunction
matr=input("Enter a matrix")
printf (string(take_detm(matr)))

Here's the problem: When I run the code and input a matrix as: [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16] the console prints 4 (the order) and the program hangs. I get that rolling blue ring of Windows 7 and after some time a message which says that Scilab 6.0.1 has stopped working. Is there any issue with the algorithm or is it something else? PS-Beginner level

hello_world
  • 29
  • 10

2 Answers2

0

The problem is due to the A(:,i)=[] instruction. assigning [] only works for a set of full rows or a set of full lines, so your instruction simply removed the ith column of the A matrix (the result being a rectangular matrix)

You can fix the problem with

    Ai=A(2:order,[1:i-1 i+1:order])//deleting 1st row and  column i
    s=s+((-1)^(i+1))*A(1,i)*take_detm(Ai); //recursive call

Note however that the Scilab det function is much more precise and efficient

user5694329
  • 412
  • 3
  • 3
  • Actually, I never understood the assignment to [ ]. I was originally trying some other method to delete columns. But it wasn't working, so I found this assignment method on YouTube (without proper explanation) and used it as a last minute resort. Any link which explains the method properly? – hello_world Nov 25 '18 at 03:09
  • I wanted to try out writing the determinant algorithm from scratch just for fun, hence I didn't use the in-built function. – hello_world Nov 25 '18 at 03:11
0

Okay, so I am confident I get this, hence answering my own question.

When you want to remove a row OR a column in Scilab, you can assign it to []. The following example illustrates how this works. Consider the matrix

A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]

which prints in the console as

A  = 

   1.    2.    3.    4. 
   5.    6.    7.    8. 
   9.    10.   11.   12.
   13.   14.   15.   16.

If you want to remove the first row, the command is

    A(1,:)=[]
 A  = 

   5.    6.    7.    8. 
   9.    10.   11.   12.
   13.   14.   15.   16.

You simply assign the first row to the empty matrix. Similarly if you wanted to remove the 2nd column, you assign it to empty matrix:

A(:,2)=[]
A  = 

   5.    7.    8. 
   9.    11.   12.
   13.   15.   16. 

(Note that the operation is performed on the updated matrix--i.e the 1st row removed) But if you want to remove the 1st row and the 2nd column, you can't write:

A(1,2)=[]

Scilab says:

Submatrix incorrectly defined.

Here is an alternate solution using assignment to the empty matrix: General idea: We perform 2 operations one by one: deleting the 1st row and then the ith column. Posting code for just the else part:

else

    s=0
    first_row_removed=A //taking a backup of A and then...
    first_row_removed(1,:)=[] //...removing the 1st row
    for i=1:order
        column_i_removed=first_row_removed //taking a backup of the 1st-row-removed matrix...
        column_i_removed(:,i)=[] //... and then deleting column i
        s=s+((-1)^(i+1))*A(1,i)*take_detm(column_i_removed); //recursive call
    end
    determinant=s

end //of else   

An important thing to note is that performing the assignment to the empty matrix makes changes in the original matrix itself. Hence for every iteration of the for loop, we must make sure that we perform remove-ith-column operation on the 1st-row-removed matrix and not on the matrix which had its ith column deleted in the previous for loop iteration. Hence the line

column_i_removed=first_row_removed

must be inside the for loop.

hello_world
  • 29
  • 10