MATLAB:比较两个数组的所有元素

发布于 2024-08-20 14:09:45 字数 515 浏览 8 评论 0原文

我在 MATLAB 中有两个矩阵,分别是 arr1arr2,大小分别为 1000*1000。我想比较它们的元素并将比较结果保存在结果矩阵 resarr 中,该矩阵也是 1000*1000,这样对于每个元素:

  • 如果 arr1 中的元素大于一个在arr2中,如果arr2中的元素更大,则将值1放入结果中,
  • 存储值2

,但我不想用for循环来做到这一点,因为那比较慢。我该怎么做?


编辑: 另外,如果我想在 1000*1000*3 结果矩阵中存储不同的 RGB 值,具体取决于 arr1 和 arr2 的比较,是否可以在没有慢循环的情况下完成?

例如,如果 arr1 较大,则存储 (255,0,0);如果 arr2 较大,则存储 (0,255,0)

I have two matrices in MATLAB lets say arr1 and arr2 of size 1000*1000 each. I want to compare their elements and save the comparison in a result matrix resarr which is also 1000*1000 such that for each element:

  • if the element in arr1 is bigger than the one in arr2, place the value 1 in the result
  • if the element in arr2 is bigger, store the value 2

but I don't want to do this with for loops because that is slower. How can I do this?


EDIT:
Also if I wanted to store different RGB values in a 1000*1000*3 result matrix, depending on the comparison of arr1 and arr2, could that be done without slow loops?

For example store (255,0,0) if arr1 is larger and (0,255,0) if arr2 is larger

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

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

发布评论

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

评论(2

热鲨 2024-08-27 14:09:45
resarr = 2 - (arr1 > arr2)

arr1>arr2 逐个元素比较 arr1 和 arr2,返回 1000x1000 矩阵,其中 arr1 较大的情况下包含 1,否则返回 0。 2 - 部分将其放入矩阵中,如果 arr1 大于 arr2,则为 1,否则为 2。

注意:如果 arr1 和 arr2 在某个时刻相等,您也会得到 2(因为 arr1>arr2 返回 0,则 2-0=2)。

resarr = 2 - (arr1 > arr2)

arr1>arr2 compares arr1 and arr2, element by element, returning 1000x1000 matrix containing 1 where arr1 is larger, and 0 otherwise. the 2 - part makes it into a matrix where there are 1's if arr1 was larger than arr2, and 2's otherwise.

note: if arr1 and arr2 are euqal at some point, you'll also get 2 (because arr1>arr2 return 0, then 2-0=2).

岁月静好 2024-08-27 14:09:45

关于您的编辑,一旦您将 resarr 矩阵计算为 Ofri 建议,您可以通过以下方式修改 RGB 矩阵 img

N = numel(resarr);  %# The number of image pixels

index = find(resarr == 1);  %# The indices where arr1 is bigger
img(index) = 255;           %# Change the red values
img(index+N) = 0;           %# Change the green values
img(index+2*N) = 0;         %# Change the blue values

index = find(resarr == 2);  %# The indices where arr2 is bigger
img(index) = 0;             %# Change the red values
img(index+N) = 255;         %# Change the green values
img(index+2*N) = 0;         %# Change the blue values

With respect to your edit, once you have your resarr matrix computed as Ofri suggested, you can modify an RGB matrix img in the following way:

N = numel(resarr);  %# The number of image pixels

index = find(resarr == 1);  %# The indices where arr1 is bigger
img(index) = 255;           %# Change the red values
img(index+N) = 0;           %# Change the green values
img(index+2*N) = 0;         %# Change the blue values

index = find(resarr == 2);  %# The indices where arr2 is bigger
img(index) = 0;             %# Change the red values
img(index+N) = 255;         %# Change the green values
img(index+2*N) = 0;         %# Change the blue values
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文