本文介绍了在 MATLAB 中进行这种 Pythonic 向量化赋值的等效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将这行代码从 Python 转换为 MATLAB:

I'm trying to translate this line of code from Python to MATLAB:

new_img[M[0, :] - corners[0][0], M[1, :] - corners[1][0], :] = img[T[0, :], T[1, :], :]

所以,很自然地,我写了这样的东西:

So, naturally, I wrote something like this:

new_img(M(1,:)-corners(2,1),M(2,:)-corners(2,2),:) = img(T(1,:),T(2,:),:);

但是当它到达该行时它给了我以下错误:

But it gives me the following error when it reaches that line:

请求的 106275x106275x3 (252.4GB) 阵列超过最大阵列大小偏爱.创建大于此限制的数组可能需要很长时间时间并导致 MATLAB 变得无响应.请参阅数组大小限制或偏好面板了解更多信息.

这让我相信它没有正确分配事物.Img 最多为 1000 倍;1500 RGB 图像.相同的代码在 Python 中的运行时间不到 5 秒.如何像 MATLAB 第一行中的代码一样进行向量赋值?

This has made me believe that it is not assigning things correctly. Img is at most a 1000 × 1500 RGB image. The same code works in less than 5 seconds in Python. How can I do vector assignment like the code in the first line in MATLAB?

顺便说一句,我没有粘贴这篇文章的所有代码行,以免太长.如果我需要添加任何其他内容,请告诉我.

By the way, I didn't paste all lines of my code for this post not to get too long. If I need to add anything else, please let me know.

这是我希望我的代码做什么的解释(基本上,这就是 Python 代码所做的):

Here's an explanation of what I want my code to do (basically, this is what the Python code does):

考虑这行代码.这不是真正的 MATLAB 代码,我只是想解释一下我想做什么:

Consider this line of code. It's not a real MATLAB code, I'm just trying to explain what I want to do:

A([2 3 5], [1 3 5]) = B([1 2 3], [2 4 6])

解释如下:

A(2,1) = B(1,2)
A(3,1) = B(2,2)
A(5,1) = B(3,2)
A(2,3) = B(1,4)
A(3,3) = B(2,4)
A(5,3) = B(3,4)
...
...
...

相反,我希望它被这样解释:

Instead, I want it to be interpreted like this:

A(2,1) = B(1,2)
A(3,3) = B(2,4)
A(5,5) = B(3,6)

推荐答案

当你在 Python 中执行 A[vector1, vector2] 时,你索引了集合:

When you do A[vector1, vector2] in Python, you index the set:

A[vector1[0], vector2[0]]
A[vector1[1], vector2[1]]
A[vector1[2], vector2[2]]
A[vector1[3], vector2[3]]
...

在 MATLAB 中,类似的 A(vector1, vector2) 代替索引集合:

In MATLAB, the similar-looking A(vector1, vector2) instead indexes the set:

A(vector1(1), vector2(1))
A(vector1(1), vector2(2))
A(vector1(1), vector2(3))
A(vector1(1), vector2(4))
...
A(vector1(2), vector2(1))
A(vector1(2), vector2(2))
A(vector1(2), vector2(3))
A(vector1(2), vector2(4))
...

也就是说,您获得了每个索引组合.您应该将其视为由两个向量中指定的行和列组成的子数组.

That is, you get each combination of indices. You should think of it as a sub-array composed of the rows and columns specified in the two vectors.

要完成与 Python 代码相同的操作,您需要使用线性索引:

To accomplish the same as the Python code, you need to use linear indexing:

index = sub2ind(size(A), vector1, vector2);
A(index)

因此,您的 MATLAB 代码应该:

Thus, your MATLAB code should do:

index1 = sub2ind(size(new_img), M(1,:)-corners(2,1), M(2,:)-corners(2,2));
index2 = sub2ind(size(img), T(1,:), T(2,:));

% these indices are for first 2 dims only, need to index in 3rd dim also:
offset1 = size(new_img,1) * size(new_img,2);
offset2 = size(img,1) * size(img,2);
index1 = index1.' + offset1 * (0:size(new_img,3)-1);
index2 = index2.' + offset2 * (0:size(new_img,3)-1);

new_img(index1) = img(index2);

中间块在这里所做的是为沿第三维的相同元素添加线性索引.如果 ii 是第一个通道中某个元素的线性索引,则 ii + offset1 是第二个通道中相同元素的索引,并且 ii +2*offset1 是第三个通道中相同元素的索引,等等.所以这里我们生成所有这些矩阵元素的索引.+ 操作正在进行隐式单例扩展(他们在 Python 中称之为广播").如果您有旧版本的 MATLAB,这将失败,您需要将 A+B 替换为 bsxfun(@plus,A,B).

What the middle block does here is add linear indexes for the same elements along the 3rd dimension. If ii is the linear index to an element in the first channel, then ii + offset1 is an index to the same element in the second channel, and ii + 2*offset1 is an index to the same element in the third channel, etc. So here we're generating indices to all those matrix elements. The + operation is doing implicit singleton expansion (what they call "broadcasting" in Python). If you have an older version of MATLAB this will fail, you need to replace that A+B with bsxfun(@plus,A,B).

这篇关于在 MATLAB 中进行这种 Pythonic 向量化赋值的等效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 01:01