Comparing large sparse matrices for differences close to machine precision

16 ビュー (過去 30 日間)
Tobias Brambier
Tobias Brambier 2022 年 9 月 21 日
編集済み: Bruno Luong 2022 年 9 月 21 日
Basically what I am doing right now is creating the same matrix k in two very different ways. As it turned out simply comparing them by the find command results in almost no matches as the single elements are somehow very slightly different. as in if I subtract one element from the other the result is either close to or at machine precision. I am now looking for a way to compare those two matrices in a way, so I get an Idea of how bad the differences are.

採用された回答

Walter Roberson
Walter Roberson 2022 年 9 月 21 日
[r1, c1, s1] = find(k1);
[r2, c2, s2] = find(k2);
rc1 = [r1, c1];
rc2 = [r2, c2];
[~, ia, ic] = intersect(rc1, rc2, 'rows');
common1 = s1(ia); common2 = s2(ic);
At this point, common1 and common2 are corresponding non-zero values that exist in both arrays, at locations k1(r1(ia(J)), c1(ia(J))) and k2(r2(ic(J)), c2(ic(J))) .. well except you should expect them to be at the same location in both.
You know these will be non-zero, so you can calculate relative change however is appropriate, such as
common2./common1 - 1 %also known as (common2 - common1)/common1

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2022 年 9 月 21 日
編集済み: Bruno Luong 2022 年 9 月 21 日
One way to compare matrices is to display
norm(A-B, Inf) / norm(A, Inf)
It should be at machine precision (~1e-15) of at worst about 1e-12 if they supposed to be identical but computed with different thread/cu/instruction order etc...

カテゴリ

Help Center および File ExchangePhased Array Design and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by