To compare to matrices element by element

121 ビュー (過去 30 日間)
Pradheep S
Pradheep S 2021 年 7 月 21 日
コメント済み: Walter Roberson 2021 年 7 月 21 日
i have matrix A= [ 1 2 2 , 4 5 6] and matrix B = [ 1 2 3 , 2 3 4 ] . want to caompare element by element martix operators and any one element or all element in A if lessthan B should return matrix A is lesser .
  1 件のコメント
Torsten
Torsten 2021 年 7 月 21 日
if A <= B
disp('A is less or equal B')
end

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

採用された回答

Mudit Chaturvedi
Mudit Chaturvedi 2021 年 7 月 21 日
Hello!
I understand you are trying to compare two matrices element by element and return "matrix A is lesser" if any element in A is lesser than its corresponding element in B.
One of the ways to do this is to create a matrix C containing the maximum of A and B (element by element) and then checking if this new matrix is same as A or not.
C = max(A,B); %contains the max value out of A and B at each index
isequal(A,C); %returns true if A and C are equal else false
You can add an if condition that returns "matrix A is lesser" when isequal() evaluates to true.
  2 件のコメント
Pradheep S
Pradheep S 2021 年 7 月 21 日
Hi thank you.
can you help me with function. for eg isqual returns True it should execute a function .
if returns False it should execute other function
Walter Roberson
Walter Roberson 2021 年 7 月 21 日
if isequal(A, C)
one_function()
else
different_function()
end

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

その他の回答 (1 件)

Jonas
Jonas 2021 年 7 月 21 日
編集済み: Jonas 2021 年 7 月 21 日
you can compare matrix A and B directly using typical comparing operators
A<B
if the question is if there is any element of A smaller than the corresponding element in B, you can use e.g.
any(A<B,'all')

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by