# how to determine similar values in 2 equally-sized matrices (for non-zero elements which are in similar positions) ?

1 ビュー (過去 30 日間)
Kate 2016 年 4 月 14 日

Hello,
I have 2 matrices:
A = [ 1 2 4;
5 0 6]
and
B = [0 2 5;
2 0 6]
I want to output a binary vector for the same values in the same col/row locations. My answer must also be for values which are non-zero.
So I would like to get as my final answer :
Final_output = [0 1 0;
0 0 1]
Currently, I use the following code, but it cannot handle cases where a 'zero' is present in similar positions on the 2 matrices.
Wrong_Final_output = (A==B)
[0 1 0;
0 1 1]
##### 1 件のコメント表示非表示 なし
Kate 2016 年 4 月 14 日
any help ?

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

### 採用された回答

Teja Muppirala 2016 年 4 月 14 日
A = [ 1 2 4;
5 0 6];
B = [0 2 5;
2 0 6];
Final_output = (A==B) & (A ~= 0)
This gives
Final_output =
0 1 0
0 0 1
You could just write this as well:
Final_output = (A==B) & A
But I think having the (A ~= 0) part in there makes it more readable.

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

### カテゴリ

Find more on Linear Algebra in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by