简体   繁体   English

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

[英]MATLAB: Extract multiple parts of a matrix without using loops

I have a huge 2D matrix and I would like to extract 15 different 100x100 parts out of it. 我有一个巨大的2D矩阵,我想从中提取15个不同的100x100部件。 I have two vectors x and y where the top left indices of the parts are saved. 我有两个向量x和y,其中保存了零件的左上角索引。 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. 但结果只是100x100矩阵而不是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. 另一种可能性是写出所有15条线,但这有点难看。

Do you have any elegant solution? 你有什么优雅的解决方案吗? Thanks. 谢谢。

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 . 大多数解决方案涉及将向量xy扩展为更大的索引矩阵,并且可能使用REPMATBSXFUNSUB2IND中的一个或多个函数。 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 : 它使用匿名函数以及函数ARRAYFUNCAT

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. 这是一个简单的单行函数,可以即时创建,而无需将其放在m文件中。 The function defines two inputs r and c which are used to extract a 100-by-100 submatrix from cam1 . 该函数定义了两个输入rc ,用于从cam1中提取100×100的子矩阵。 The variable indexFcn stores a function handle which is used to call the function. 变量indexFcn存储用于调用函数的函数句柄 Note that the values of cam1 used by the anonymous function are static . 请注意,匿名函数使用的cam1的值是静态的 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. 即使变量cam1中的值发生更改,匿名函数仍会使用创建函数时cam1中的值。

The second line makes a call to ARRAYFUN, which applies a function to every element of an array. 第二行调用ARRAYFUN,它将函数应用于数组的每个元素。 ARRAYFUN loops over each entry in x and y , passing the values to indexFcn . ARRAYFUN循环遍历xy中的每个条目,将值传递给indexFcn The output is stored in result , a 15-element cell array where each cell contains a 100-by-100 matrix. 输出存储在结果中 ,这是一个15个元素的单元阵列,其中每个单元包含一个100乘100的矩阵。

The third line uses the CAT function to concatenate the 100-by-100 matrices into a 100-by-100-by-15 matrix. 第三行使用CAT函数将100×100矩阵连接成100×100×15的矩阵。

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 将从A产生2x2子矩阵。但我们也可以找到子矩阵为

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. 要获得答案,您需要阅读有关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. 最后,看起来你想要从提取的部分获得15x100x100阵列。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM