Why do i get the error (Array indices must be positive integers or logical values) when trying to calculate the determinant

34 views (last 30 days)
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'
r3=datat(3,2)
disp (r3)
r2=datat(2,:)
disp (r2)
P1= r3 * r2
M=diag(P1); %i did this so P1 has the right format
det = det(M) %this is where i get the error

Answers (3)

Walter Roberson
Walter Roberson on 3 Nov 2024 at 21:32
det = det(M)
There would not be any error the first time you ran that code. The first time, det would refer to the det determinant function.
However, the second time you ran the code, det would refer to the variable you just created by assigning to det and det(M) would be a request to index the variable, which would fail.

Torsten
Torsten on 3 Nov 2024 at 20:46
Edited: Torsten on 3 Nov 2024 at 20:46
I don't get any error. But you shouldn't overwrite the function "det" by using the variable name "det" for the result.
Name it determinant, e.g.:
determinant = det(M)
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';
r3=datat(3,2);
disp (r3)
5.2000
r2=datat(2,:);
disp (r2)
0.5400 4.6000 4.0000
P1= r3 * r2;
M=diag(P1); %i did this so P1 has the right format
det = det(M) %this is where i get the error
det = 1.3971e+03

Umar
Umar on 3 Nov 2024 at 21:25

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/symbolic/sym.cond.html?searchHighlight=cond&s_tid=srchtitle_support_results_2_cond

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.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!