Why do i get the error (Array indices must be positive integers or logical values) when trying to calculate the determinant
0 Comments
Answers (3)
0 Comments
0 Comments
Hi @BICU,
You have a matrix data that you transpose into datat. From this transposed matrix, you extract specific elements (r3 and r2) to compute a new vector P1. Then, you create a diagonal matrix M from this vector and compute its determinant using MATLAB's det() function.
https://www.mathworks.com/help/matlab/ref/det.html
You seek clarification on whether the determinant calculated (det = 1.3971e+03) is accurate or if there is an error in your approach. Please see attached.
Let me help you understand by breaking down your code.
Data Initialization
data = [ 3.88, 0.54, 2.34, 3.00; 18.00, 4.60, 5.20, 6.00; 23.00, 4.00, 7.40, -2.00 ]; datat = data';
This creates a transposed matrix where the rows become columns.
Extracting Values
r3 = datat(3,2); % This retrieves the element at row 3, column 2 r2 = datat(2,:); % This retrieves the entire second row
Here, r3 is assigned the value 5.20, and r2 becomes a row vector [0.54, 4.60, 4.00]
Calculating P1
P1 = r3 * r2; % Multiplying scalar r3 with row vector r2
The result is a row vector: P1 = [5.20 times 0.54, 5.20 times 4.60, 5.20 times 4.00] = [2.8080, 23.9200, 20.8000]
Creating Diagonal Matrix
M = diag(P1); % Constructs a diagonal matrix from P1
Calculating Determinant
det = det(M); % Computes the determinant of the diagonal matrix M
For a diagonal matrix M constructed from vector P1, the determinant can be calculated as the product of its diagonal elements:
de(M) = P1 times P1(2) times P1(3) = 2.8080 times 23.9200 times 20.8000
This multiplication indeed yields approximately 1.3971e+03 confirming that your calculation is accurate. Your code correctly computes the determinant of the diagonal matrix derived from P1. The output you received (det = 1.3971e+03) is indeed correct based on your calculations. If you noted in the Matrix documentation provided, while determinants can give insights into matrix properties (like singularity), they can be sensitive to numerical errors and should not be relied upon solely for determining singularity or conditioning of matrices. If you are concerned about singularity or numerical stability in matrices in future applications, consider using functions like cond() or rcond() for more reliable assessments.
https://www.mathworks.com/help/matlab/ref/rcond.html?s_tid=doc_ta
This thorough breakdown should clarify any doubts regarding your MATLAB code and the accuracy of the determinant computed therein!
Hope this helps.
0 Comments
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!