两个矩阵的高效比较 MATLAB

发布于 2024-10-21 08:17:13 字数 148 浏览 1 评论 0原文

有没有一种方法可以有效地比较两个矩阵,我在想类似

same = abs((AB)) = 0...
将一个矩阵的值减去另一个矩阵的值,如果结果为 0,则它们是相同的,还有一个 isequal() 函数,比较两个矩阵的最佳方法是什么?

Is there a way to efficiently compare two matrices, I was thinking something like

same = abs((A-B)) = 0...
substracting values of one matrix to the other and if they result is 0, they are the same, also there is a isequal() function, What would be the best to compare both matrices?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゃ懵逼小萝莉 2024-10-28 08:17:13

您只需执行 isequal(A,B) 即可,如果 true 则返回 1 ,如果 false 则返回 0

You can simply do isequal(A,B) and it will return 1 if true or 0 if false.

无人问我粥可暖 2024-10-28 08:17:13

由于您正在处理浮点,因此您可能不想测试精确的相等性(取决于您的应用程序)。因此,您只需检查它

norm(A - B)

是否足够小,例如 < 1e-9,同样取决于您的应用程序。这是矩阵 2-范数,如果 A - B 是全零矩阵或接近零矩阵,则该矩阵将接近零。

Since you're dealing with floating point, you probably don't want to test for exact equality (depending on your application). Thus, you can just check that

norm(A - B)

is sufficiently small, say < 1e-9, again depending on your application. This is the matrix 2-norm, which will be near zero if A - B is the all zeros matrix or nearly so.

心如狂蝶 2024-10-28 08:17:13

似乎 ISEQUAL 比检查减法后的非零元素更快:

>> a = rand(100, 100);
>> b = a;
>> tic; for ii = 1:100000; any(any(a - b)); end; toc;
Elapsed time is 2.089838 seconds.
>> tic; for ii = 1:100000; isequal(a, b); end; toc;
Elapsed time is 1.201815 seconds.

It seems that ISEQUAL is faster than checking for non-zero elements after subtraction:

>> a = rand(100, 100);
>> b = a;
>> tic; for ii = 1:100000; any(any(a - b)); end; toc;
Elapsed time is 2.089838 seconds.
>> tic; for ii = 1:100000; isequal(a, b); end; toc;
Elapsed time is 1.201815 seconds.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文