MATLAB:在不使用循环的情况下提取矩阵的多个部分

发布于 2024-07-22 02:26:04 字数 336 浏览 5 评论 0原文

我有一个巨大的 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 技术交流群。

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

发布评论

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

评论(3

无悔心 2024-07-29 02:26:04

有多种方法可以在不使用循环的情况下完成此操作。 大多数解决方案涉及将向量xy扩展为更大的索引矩阵,并且可能会使用一个或多个函数REPMATBSXFUN,或 SUB2IND。 可以在此处找到矩阵索引的好教程。

然而,由于您要求一种优雅的解决方案,所以这里有一个有点不寻常的解决方案。 它使用匿名函数以及函数ARRAYFUNCAT

indexFcn = @(r,c) cam1(r:(r+99),c:(c+99));
result = arrayfun(indexFcn,x,y,'UniformOutput',false);
result = cat(3,result{:});

说明:

第一行创建一个匿名功能。 这是一个简单的单行函数,可以即时创建,而无需将其放入 m 文件中。 该函数定义了两个输入 rc,用于从 cam1 中提取 100×100 子矩阵。 变量indexFcn存储函数句柄< /a> 用于调用该函数。 请注意,匿名函数使用的 cam1 值是静态。 即使变量 cam1 中的值发生更改,匿名函数仍使用创建该函数时 cam1 中的值。

第二行调用 ARRAYFUN,它将函数应用于数组的每个元素。 ARRAYFUN 循环遍历 xy 中的每个条目,将值传递给 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:

indexFcn = @(r,c) cam1(r:(r+99),c:(c+99));
result = arrayfun(indexFcn,x,y,'UniformOutput',false);
result = cat(3,result{:});

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.

嘦怹 2024-07-29 02:26:04

由于这显然是作业,我不会给你完整的答案。

有多种方法可以对矩阵进行索引。 当您有这样的分散索引集时,您需要使用单个索引。 因此,如果

A = rand(5,6)
A =
      0.81472      0.09754      0.15761      0.14189      0.65574      0.75774
      0.90579       0.2785      0.97059      0.42176     0.035712      0.74313
      0.12699      0.54688      0.95717      0.91574      0.84913      0.39223
      0.91338      0.95751      0.48538      0.79221      0.93399      0.65548
      0.63236      0.96489      0.80028      0.95949      0.67874      0.17119

A(3:4,3:4)

将从 A 中产生一个 2x2 子矩阵。但我们也可以找到该子矩阵:

reshape(A([13 14 18 19]),[2 2])
ans =
      0.95717      0.91574
      0.48538      0.79221

为什么我选择这个索引集? 要找到答案,您需要阅读有关 sub2ind 的内容。

[I,J] = ndgrid(3:4,3:4);
sub2ind([5 6],I(:),J(:))
ans =
    13
    14
    18
    19

最后,看起来您想要从提取的部分中获得一个 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

A = rand(5,6)
A =
      0.81472      0.09754      0.15761      0.14189      0.65574      0.75774
      0.90579       0.2785      0.97059      0.42176     0.035712      0.74313
      0.12699      0.54688      0.95717      0.91574      0.84913      0.39223
      0.91338      0.95751      0.48538      0.79221      0.93399      0.65548
      0.63236      0.96489      0.80028      0.95949      0.67874      0.17119

A(3:4,3:4)

will yield a 2x2 submatrix from A. But we can also find that submatrix as

reshape(A([13 14 18 19]),[2 2])
ans =
      0.95717      0.91574
      0.48538      0.79221

Why did I choose this index set? For the answer, you will need to read about sub2ind.

[I,J] = ndgrid(3:4,3:4);
sub2ind([5 6],I(:),J(:))
ans =
    13
    14
    18
    19

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.

不寐倦长更 2024-07-29 02:26:04

你认为这一切都太困难了,试试这个:mat2cell

You think all far to difficult, try this one: mat2cell

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