14 views (last 30 days)

Produce a random 3x3 matrix A that is invertible and display it. Hint: Use a while-loop until you get one with non-zero determinant. To create a random matrix with N rows and M columns,use the MATLAB command rand(N,M).

I know how to produce the 3x3 matrix and how to display it. I am just a little confused on how to use the while-loop. Can someone please help me?

Jeremy Marcyoniak
on 21 Jan 2020

n = 3; m = 3;

A = zeros(n,m);

while det(A) == 0

A = rand(n,m);

end

Jeremy Marcyoniak
on 21 Jan 2020

"Use a while-loop until you get one with non-zero determinant"

The problem asks for an invertible matrix, meaning that the inverse exists. You are looking for a determinant that is not equal to zero.

A zero determinant would not be an invertible matrix (i.e., the inverse does not exist because the determinant is zero).

Jeremy Marcyoniak
on 21 Jan 2020

Using rand, it's fairly unlikely that you'll create a matrix whose determinant is 0. It'd probably be easier to get a 0 determinant with randi

That being said, maybe running this will help you see what's going on

n = 3; m = 3;

A = zeros(n,m);

k = 0;

while det(A) == 0

A = rand(n,m);

k = k + 1;

fprintf(1,'\nIteration Number %i \n',k)

fprintf(1,' Matrix A = \n\n')

disp(A)

fprintf(1,'The Determinant of Matrix A is: \n')

disp(det(A))

end

And if you wanted an (approximately) zero determinant:

n = 3; m = 3;

A = eye(n,m);

k = 0;

while abs(det(A)) > 0.001

A = rand(n,m);

k = k + 1;

fprintf(1,'\nIteration Number %i \n',k)

fprintf(1,' Matrix A = \n\n')

disp(A)

fprintf(1,'The Determinant of Matrix A is: \n')

disp(det(A))

end

Using randi instead:

n = 3; m = 3;

A = eye(n,m);

k = 0;

while det(A) ~= 0

A = randi([0 10],n,m);

k = k + 1;

fprintf(1,'\nIteration Number %i \n',k)

fprintf(1,' Matrix A = \n\n')

disp(A)

fprintf(1,'The Determinant of Matrix A is: \n')

disp(det(A))

end

I hope this helps

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501303-3x3-matrix-with-while-loop#comment_788391

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501303-3x3-matrix-with-while-loop#comment_788391

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501303-3x3-matrix-with-while-loop#comment_788506

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/501303-3x3-matrix-with-while-loop#comment_788506

Sign in to comment.