如何比较两个数组的所有元素?

发布于 2024-08-20 11:21:58 字数 123 浏览 7 评论 0原文

我有两个大约 1000 行和 1000 列的大数组。我需要比较这些数组的每个元素,如果相应的元素相等,则将 1 存储在另一个数组中。

我可以用 for 循环来做到这一点,但这需要很长时间。我怎样才能更快地做到这一点?

I have two big arrays with about 1000 rows and 1000 columns. I need to compare each element of these arrays and store 1 in another array if the corresponding elements are equal.

I can do this with for loops but that takes a long time. How can I do this faster?

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

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

发布评论

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

评论(3

心的憧憬 2024-08-27 11:21:58

给出的答案都是正确的。我只是想详细说明gnovice的评论关于浮点测试。

比较浮点数是否相等时,需要使用容差值。常用的公差比较有两种:绝对公差和相对公差。 (来源

绝对容差ab 的比较如下所示:

|a-b| < tol

相对容差比较如下所示:

|a-b| < tol*max(|a|,|b|) + tol_floor

您可以将上述两个函数实现为匿名函数:

%# absolute tolerance equality
isequalAbs = @(x,y,tol) ( abs(x-y) <= tol );

%# relative tolerance equality
isequalRel = @(x,y,tol) ( abs(x-y) <= ( tol*max(abs(x),abs(y)) + eps) );

然后您可以将它们用作:

%# let x and y be scalars/vectors/matrices of same size
x == y
isequalAbs(x, y, 1e-6)
isequalRel(x, y, 1e-6)

The answers given are all correct. I just wanted to elaborate on gnovice's remark about floating-point testing.

When comparing floating-point numbers for equality, it is necessary to use a tolerance value. Two types of tolerance comparisons are commonly used: absolute tolerance and relative tolerance. (source)

An absolute tolerance comparison of a and b looks like:

|a-b| < tol

A relative tolerance comparison looks like:

|a-b| < tol*max(|a|,|b|) + tol_floor

You can implement the above two as anonymous functions:

%# absolute tolerance equality
isequalAbs = @(x,y,tol) ( abs(x-y) <= tol );

%# relative tolerance equality
isequalRel = @(x,y,tol) ( abs(x-y) <= ( tol*max(abs(x),abs(y)) + eps) );

Then you can use them as:

%# let x and y be scalars/vectors/matrices of same size
x == y
isequalAbs(x, y, 1e-6)
isequalRel(x, y, 1e-6)
べ映画 2024-08-27 11:21:58

如果您的两个矩阵 AB 大小相同,那么您可以这样做:

index = A == B;

并且 index 将是 逻辑数组,其中 1 处处都是 A 的元素和 B 相等且为零。

警告...

如果AB包含整数,上面的应该没问题。但是,如果它们包含浮点值,您可能会得到不需要的结果。对于完全相等的元素,上面的代码只会有值1。即使是最小的差异也会导致元素被视为不相等。

您可以查看此问题的答案 有关处理“浮点运算的危险”的更多信息。一种解决方案是检查数组元素是否在彼此给定的容差范围内,如下所示:

tolerance = 0.0001;
index = abs(A-B) <= tolerance;

上面将为您提供一个逻辑数组 index ,其中 AB 彼此相差在 0.0001 以内,否则为零。

If your two matrices A and B are the same size, then you can do this:

index = A == B;

and index will be a logical array with ones everywhere an element of A and B are equal and zero otherwise.

A word of warning...

If A and B contain integers, the above should be fine. However, if they contain floating point values, you may get undesired results. The above code will only have values of one for elements that are exactly equal. Even the smallest difference will cause the elements to be considered unequal.

You can look at this question's answers for more information about dealing with the "perils of floating point operations". One solution would be to check that array elements are within a given tolerance of one another, like so:

tolerance = 0.0001;
index = abs(A-B) <= tolerance;

The above will give you a logical array index with ones everywhere the elements of A and B are within 0.0001 of each other and zero otherwise.

喜你已久 2024-08-27 11:21:58

只需使用普通的 == 运算符即可:

>> [1 2; 3 4] == [1 5; 6 4]      

ans =

     1     0
     0     1

Just use the normal == operator:

>> [1 2; 3 4] == [1 5; 6 4]      

ans =

     1     0
     0     1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文