Function: Compare each Element of 2 Matrix for zero and non-zero

4 ビュー (過去 30 日間)
Xenofon Antoniou
Xenofon Antoniou 2019 年 12 月 7 日
コメント済み: Xenofon Antoniou 2019 年 12 月 7 日
Hi All,
I am looking at writing a function for the below set of constraints.
We start with a matrix, Z
We want to compare zero and non-zero elements of Z and Z^2.
Hence, need to write a function that will return:
- True if all non-zero elements in a Matrix, Z^2, are accompanied by non-zero (true) elements at the same locations in matrix Z.
As an example:
Z = [1,0,1;1,1,1;1,0,1]
Z^2 = [2,0,2;3,1,3;2,0,2]
From this, we can see if we compare Z and Z^2 by element, all non-zero elements of Z^2 are accompanied by non-zero in Z.
Hence, the function, would be TRUE.

採用された回答

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 12 月 7 日
Z = [1,0,1;1,1,1;1,0,1]
Z_2 = [2,0,2;3,1,3;2,0,2]
answer=sum(sum(Z_2(Z==0)))==0;%1 if it is true
  1 件のコメント
Xenofon Antoniou
Xenofon Antoniou 2019 年 12 月 7 日
Hi Jesus,
These are generic matricies and not saved in the workspace.
Ideally it would be written as a function.
function answer = ABC(Z)
answer=sum(sum(Z^2,(Z==0)))==0;
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by