MATLAB Answers

Reversible matrix in matlab

13 ビュー (過去 30 日間)
Noa  Yelin
Noa Yelin 2020 年 10 月 20 日
コメント済み: Jan 2020 年 10 月 21 日
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 件のコメント

表示 4 件の古いコメント
Noa  Yelin
Noa Yelin 2020 年 10 月 20 日
Regular matrix and non-singular matrix
KSSV
KSSV 2020 年 10 月 20 日
What is sz in the function? It will throw error.
Noa  Yelin
Noa Yelin 2020 年 10 月 20 日
this is how I create array of 0

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2020 年 10 月 20 日
You need to take that code for irr_mat and store it in file irr_mat.m

  13 件のコメント

表示 10 件の古いコメント
Noa  Yelin
Noa Yelin 2020 年 10 月 20 日
any help?
Steven Lord
Steven Lord 2020 年 10 月 20 日
You asked a continuation question here. In the future, please keep discussions of the same issue in one Answers post.
Noa  Yelin
Noa Yelin 2020 年 10 月 20 日
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)

サインインしてコメントする。


Jan
Jan 2020 年 10 月 20 日
編集済み: Jan 2020 年 10 月 21 日
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 件のコメント

表示 2 件の古いコメント
Noa  Yelin
Noa Yelin 2020 年 10 月 20 日
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)
Walter Roberson
Walter Roberson 2020 年 10 月 20 日
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 2020 年 10 月 21 日
@Noa Yelin: I had a bug in the code, which is fixed now. See my answer.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by