MATLAB:在不使用循环的情况下提取矩阵的多个部分
我有一个巨大的 2D 矩阵,我想从中提取 15 个不同的 100x100 部分。 我有两个向量 x 和 y,其中保存了零件的左上角索引。 我使用了类似的东西:
result = cam1(x(1:end):(x(1:end)+99), y(1:end):(y(1:end)+99));
但结果只是一个 100x100 矩阵而不是 15x100x100。 为什么?
我知道使用循环可以轻松完成它,但我们不允许使用循环(这是图像处理练习的一部分)。 另一种可能性是写出所有 15 行,但这有点难看。
你有什么优雅的解决方案吗? 谢谢。
I have a huge 2D matrix and I would like to extract 15 different 100x100 parts out of it. I have two vectors x and y where the top left indices of the parts are saved. I have used something like this:
result = cam1(x(1:end):(x(1:end)+99), y(1:end):(y(1:end)+99));
but the result is just a 100x100 matrix instead of a 15x100x100. Why?
I know it could easily be done using a loop but we are not allowed to use loops (it's part of an image processing exercise). Another possbility would be to write all the 15 lines but that is kind of ugly.
Do you have any elegant solution? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有多种方法可以在不使用循环的情况下完成此操作。 大多数解决方案涉及将向量x和y扩展为更大的索引矩阵,并且可能会使用一个或多个函数REPMAT,BSXFUN,或 SUB2IND。 可以在此处找到矩阵索引的好教程。
然而,由于您要求一种优雅的解决方案,所以这里有一个有点不寻常的解决方案。 它使用匿名函数以及函数ARRAYFUN 和 CAT:
说明:
第一行创建一个匿名功能。 这是一个简单的单行函数,可以即时创建,而无需将其放入 m 文件中。 该函数定义了两个输入 r 和 c,用于从 cam1 中提取 100×100 子矩阵。 变量indexFcn存储函数句柄< /a> 用于调用该函数。 请注意,匿名函数使用的 cam1 值是静态。 即使变量 cam1 中的值发生更改,匿名函数仍使用创建该函数时 cam1 中的值。
第二行调用 ARRAYFUN,它将函数应用于数组的每个元素。 ARRAYFUN 循环遍历 x 和 y 中的每个条目,将值传递给 indexFcn。 输出存储在 result 中,这是一个 15 元素元胞数组,其中每个元胞包含一个 100×100 矩阵。
第三行使用 CAT 函数将 100×100 矩阵连接成 100×100×15 矩阵。
There are a number of ways you could do this without loops. Most solutions involve expanding the vectors x and y into larger matrices of indices and would likely use one or more of the functions REPMAT, BSXFUN, or SUB2IND. A good tutorial for matrix indexing can be found here.
However, since you asked for an elegant solution, here's one that's somewhat unusual. It uses anonymous functions as well as the functions ARRAYFUN and CAT:
EXPLANATION:
The first line creates an anonymous function. This is a simple one line function that can be created on-the-fly without having to put it in an m-file. The function defines two inputs r and c which are used to extract a 100-by-100 submatrix from cam1. The variable indexFcn stores a function handle which is used to call the function. Note that the values of cam1 used by the anonymous function are static. Even if the values in the variable cam1 change, the anonymous function still uses the values that were in cam1 when the function was created.
The second line makes a call to ARRAYFUN, which applies a function to every element of an array. ARRAYFUN loops over each entry in x and y, passing the values to indexFcn. The output is stored in result, a 15-element cell array where each cell contains a 100-by-100 matrix.
The third line uses the CAT function to concatenate the 100-by-100 matrices into a 100-by-100-by-15 matrix.
由于这显然是作业,我不会给你完整的答案。
有多种方法可以对矩阵进行索引。 当您有这样的分散索引集时,您需要使用单个索引。 因此,如果
将从 A 中产生一个 2x2 子矩阵。但我们也可以找到该子矩阵:
为什么我选择这个索引集? 要找到答案,您需要阅读有关 sub2ind 的内容。
最后,看起来您想要从提取的部分中获得一个 15x100x100 的数组。 因此,根据我所展示的部分构建必要的索引数组。 最后您需要进行最终的重塑,使其成为正确的形状。
这应该给你足够的开始来完成你的作业。
Since this is obviously homework, I won't give you the complete answer.
There are several ways to index into a matrix. When you have a scattered index set as this is, you need to use a single index. Thus if
will yield a 2x2 submatrix from A. But we can also find that submatrix as
Why did I choose this index set? For the answer, you will need to read about sub2ind.
In the end, it looks like you want a 15x100x100 array from the extracted parts. So build the necessary index array up from the pieces I've shown. You will need to do a final reshape at the end to make it the proper shape.
This should give you enough of a start to finish your homework.
你认为这一切都太困难了,试试这个:mat2cell
You think all far to difficult, try this one: mat2cell