Info

この質問は閉じられています。 編集または回答するには再度開いてください。

A function that calculates how many entries' value and location are the same between two matrices

1 回表示 (過去 30 日間)
Kevin Pamaran
Kevin Pamaran 2013 年 3 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I would like some guidance on how I can create a function that calculates how many entries' value and location are the same between two matrices.
For example
If I have A = [2 8 3; 4 8 9; 2 8 9] and B = [1 8 4; 3 8 6; 2 8 7]
How can I create a function that displays that the number of matching entries are 4? (in the example, it would be the 8's along the 2nd column and the 2 on (3,1); they have the same value and location)
edit: correction made

回答 (2 件)

Anand
Anand 2013 年 3 月 19 日
Shouldn't the answer be 4? The 2's at location(3,1) also match.

Anand
Anand 2013 年 3 月 19 日
Try this:
nnz(A==B)
Ideally, you should be taking into account differences arising from floating point precision by defining some tolerance as follows:
nnz(abs(A-B)<100*eps)

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by