Question: *Please translate to MATLAB code* // C++ program for the above approach #include using namespace std; // Function to swap values void swap(float& i, float&
*Please translate to MATLAB code*
// C++ program for the above approach #include using namespace std; // Function to swap values void swap(float& i, float& j) { float temp = i; i = j; j = temp; } // Function to find the determinant // of matrix M[][] float determinantOfMatrix( vector > mat, int N) { float mul = 1; // Iterate over N while N > 2 while (N > 2) { // Store the reduced matrix // of dimension (N-1)x(N-1) float M[N - 1][N - 1]; int next_index = 1; // Check if first element // of first row is zero while (mat[0][0] == 0) { if (mat[next_index][0] > 0) { // For swapping for (int k = 0; k < N; k++) { swap(mat[0][k], mat[next_index][k]); } // Update mul mul = mul * pow((-1), (next_index)); } else if (next_index == (N - 1)) return 0; next_index++; } // Store the first element // of the matrix float p = mat[0][0]; // Multiply the mul by // (1/p) to the power n-2 mul = mul * pow(1 / p, N - 2); // Calculate the next matrix // of dimension (N-1) x (N-1) for (int i = 1; i < N; i++) { for (int j = 1; j < N; j++) { // Calculate each element of // the matrix from previous // matrix M[i - 1][j - 1] = mat[0][0] * mat[i][j] - mat[i][0] * mat[0][j]; } } // Copy elements of the matrix // M into mat to use it in // next iteration for (int i = 0; i < (N - 1); i++) { for (int j = 0; j < (N - 1); j++) { mat[i][j] = M[i][j]; } } // Decrement N by one N--; } // Calculate the determinant // of reduced 2x2 matrix and // multiply it with factor mul float D = mul * (mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]); // Print the determinant cout << D; } // Driver Code int main() { // Given matrix vector > mat = { { 1, 0, 2, -1 }, { 3, 0, 0, 5 }, { 2, 1, 4, -3 }, { 1, 0, 5, 0 } }; // Size of the matrix int N = mat.size(); // Function Call determinantOfMatrix(mat, N); return 0; }