MATLAB Answers

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

1 ビュー (過去 30 日間)
Xenofon Antoniou
Xenofon Antoniou 2019 年 12 月 7 日
Edited: Walter Roberson 2019 年 12 月 7 日
Hi There,
I want to create a logical function that returns true if all non-zero elements in Matrix Z^2 are accompanied by non-zero (true) elements at the same locations in Z.
For example;
Z = [1,0,1;1,1,1;1,0,1]
Z^2 = [2,0,2;3,1,3;2,0,2]
From this basic example, every non-zero element in Z^2 is accompanied by a non-zero in Z. Hence the function output would be TRUE.
for i=1:N
for j=1:M
function output = ABC(Z)
output = (Z(i,j) & Z^2(i,j) ~= 0) then TRUE
end
or something to that effect.
Thanks, Xen

  0 件のコメント

サインイン to comment.

件の回答 (1)

Walter Roberson
Walter Roberson 2019 年 12 月 7 日
Z2 = Z^2;
output = all(all(~Z2 | Z));

  3 件のコメント

Xenofon Antoniou
Xenofon Antoniou 2019 年 12 月 7 日
Hi Walter,
THanks for the answer and feedback.
The code doesnt seem to give the correct answer.
For example:
A = [3,0,0;1,0,2;1,0,0]
A^2 = B = [9,0,0;5,0,0;3,0,0]
Here, you can clearly see that in row2,column3 we have the value "2".
But for A^2 = B in the same posiiton we have "0".
Thus, i want this output to equal "False" or 0.
When using the above code, we get:
output = all(all(~B | A))
Ans = 1 (TRUE).
Walter Roberson
Walter Roberson 2019 年 12 月 7 日
You stated,
"I want to create a logical function that returns true if all non-zero elements in Matrix Z^2 are accompanied by non-zero (true) elements at the same locations in Z."
If you look at B, the first column is all non-zero and the other two columns are all 0. The non-zero entries in B are the first column. Each entry in the first column of A is also non 0. Whether there are non-zero entries in other columns of A is not relevant because no other column of B is non-zero and only the non-zero locations in B are important.
Xenofon Antoniou
Xenofon Antoniou 2019 年 12 月 7 日
Hi Walter,
You are 100% correct. Thanks for the code and feedback.
Cheers, Xen

サインイン to comment.

サインイン してこの質問に回答します。


Translated by