MATLAB:在不使用循环的情况下提取矩阵的多个部分 [英] MATLAB: Extract multiple parts of a matrix without using loops

查看:281
本文介绍了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行,但这有点丑陋.

您有什么优雅的解决方案吗?谢谢.

解决方案

有多种方法可以实现无循环.大多数解决方案都涉及将向量 x y 扩展到更大的索引矩阵,并且可能会使用一个或多个函数 SUB2IND .可以在此处找到很好的矩阵索引教程.. >

但是,由于您要求的是优雅解决方案,因此这有些不寻常.它使用匿名函数以及这些函数 ARRAYFUN 函数句柄用于调用该函数.请注意,匿名函数使用的 cam1 的值为 static .即使变量 cam1 中的值发生了变化,匿名函数仍会使用创建该函数时 cam1 中的值.

第二行调用ARRAYFUN,它将对数组的每个元素应用一个函数. ARRAYFUN遍历 x y 中的每个条目,并将值传递给 indexFcn .输出结果存储在结果中,该结果是一个由15个元素组成的单元格数组,其中每个单元格包含100×100矩阵.

第三行使用CAT函数将100×100矩阵连接成100×100×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.

解决方案

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.

这篇关于MATLAB:在不使用循环的情况下提取矩阵的多个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆