Reversible matrix in matlab

Hey
I need to write a function that the user enters a matrix, and the function returns "1" if the matrix is reversible and "0" if it's not.
I wrote this function-
function x=irr_mat(A)
if A==zeros(sz,'like',p)
x=0;
else
if size(A,1)==size(A,2)
x=1;
else
x=0;
end
end
end
and when I call the function-
A=randi([1,100],4);
irr_mat(A)
I get a note that -
Undefined function 'irr_mat' for input arguments of type 'double'.
Error in Untitled5 (line 3)
irr_mat(A)
can someone help me please?

7 Commenti

What filename did you store the code in?
p is not defined in your function by the way.
Noa  Yelin
Noa Yelin il 20 Ott 2020
I don't understand...
KSSV
KSSV il 20 Ott 2020
What do you mean by a matrix is reversible?
Noa  Yelin
Noa Yelin il 20 Ott 2020
Regular matrix and non-singular matrix
KSSV
KSSV il 20 Ott 2020
What is sz in the function? It will throw error.
Noa  Yelin
Noa Yelin il 20 Ott 2020
this is how I create array of 0

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 20 Ott 2020

1 voto

You need to take that code for irr_mat and store it in file irr_mat.m

13 Commenti

Noa  Yelin
Noa Yelin il 20 Ott 2020
how i do this?
At the MATLAB command line give the command
edit irr_mat
The editor will open. In the page it creates, copy out that function code you posted, and paste it into the editor session. Now click on the menu entry to save the file.
i did this, and than get this note -
Error in Untitled5 (line 2)
irr_mat(A)
Noa  Yelin
Noa Yelin il 20 Ott 2020
any help please?
Jan
Jan il 20 Ott 2020
Please post the complete error message.
Error in irr_mat (line 3)
if A==zeros(n)
Error in Untitled5 (line 3)
irr_mat(A)
Walter Roberson
Walter Roberson il 20 Ott 2020
Modificato: Walter Roberson il 20 Ott 2020
What is n in the code?
Also change the test to
if isequal(A,zeros(n))
Noa  Yelin
Noa Yelin il 20 Ott 2020
n is the array of matrix
it doesn't work with this -
if isequal(A,zeros(n))
i got this note -
Error in irr_mat (line 3)
if isequal(A,zeros(n))
Error in Untitled5 (line 3)
irr_mat(A)
Noa  Yelin
Noa Yelin il 20 Ott 2020
n is the array of matrix
Noa  Yelin
Noa Yelin il 20 Ott 2020
any help?
Steven Lord
Steven Lord il 20 Ott 2020
You asked a continuation question here. In the future, please keep discussions of the same issue in one Answers post.
of this is right??
function x=irr_mat(A)
n=size(A)
if isequal(A,zeros(n))
x=0;
else
if size(A,1)==size(A,2)
x=1;
else
x=0;
end
end
end
and I call the function -
A=randi([6,100],2,2);
irr_mat(A)

Accedi per commentare.

Jan
Jan il 20 Ott 2020
Modificato: Jan il 21 Ott 2020
You want to identify a "regular matrix and non-singular matrix". Then testing only, if it is square and not a zero matrix ist not sufficient. Even matrices with non-zero elements can be singular.
It is hard to guess, if your code is "correct", because you do not explain what you try to achieve. Therefore just some hints:
n=size(A)
if isequal(A,zeros(n))
is equivalent to:
if any(A)
So your code canbe simplified to:
function x = irr_mat(A)
if any(A(:)) && size(A, 1) == size(A, 2) && ismatrix(A) % BUGFIX: any(A) -> any(A(:))
x = 1;
else
x = 0;
end
end
% Or shorter:
function x = irr_mat(A)
x = (any(A) && size(A, 1) == size(A, 2) && ismatrix(A));
end
But this is not a hard test for singularity.

5 Commenti

Noa  Yelin
Noa Yelin il 20 Ott 2020
Invertible matrix-
In linear algebra, an n-by-n square matrix A is called invertible (also nonsingular or nondegenerate), if there exists an n-by-n square matrix B such that A B = B A = I n {\displaystyle \mathbf {AB} =\mathbf {BA} =\mathbf {I} _{n}\ }
where In denotes the n-by-n identity matrix and the multiplication used is ordinary matrix multiplication. If this is the case, then the matrix B is uniquely determined by A, and is called the (multiplicative) inverse of A, denoted by A−1.[1][2] Matrix inversion is the process of finding the matrix B that satisfies the prior equation for a given invertible matrix A.
I need to write a function that the user enters a matrix, and the function returns "1" if the matrix is reversible and "0" if it's not.
and that code -
function x = irr_mat(A)
if any(A) && size(A, 1) == size(A, 2) && ismatrix(A)
x = 1;
else
x = 0;
end
end
doesn't work.. this note -
Error in shortmat (line 2)
if any(A) && size(A, 1) == size(A, 2) && ismatrix(A)
now i have changed the code to this -
function x=withdet(b)
if det(b)~=0 && size(b,1)==size(b,2)
x=1;
else
x=0;
end
end
and it works only when i call to function with one number -
b=randi([0,12],4);
withdet(b)
but when I call to the function like this -
b=randi([0,12],4,8);
withdet(b)
i get this note -
Error in withdet (line 2)
if det(b)~=0 && size(b,1)==size(b,2)
Error in Untitled9 (line 3)
withdet(b)
You need to post complete error messages indicating what the error is, not just where it is. For example is it complaining that there is no such function det for inputs of class DynamicSystem ?
Reminder: det() will generate an error if it is passed something that is not a square matrix, so you should never call det() until after you have ensure that the matrix is square.
Jan
Jan il 21 Ott 2020
@Noa Yelin: I had a bug in the code, which is fixed now. See my answer.

Accedi per commentare.

Categorie

Scopri di più su Linear Algebra in Centro assistenza e File Exchange

Prodotti

Richiesto:

il 20 Ott 2020

Commentato:

Jan
il 21 Ott 2020

Community Treasure Hunt

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

Start Hunting!

Translated by