定义二维坐标的两个数组,作为数组索引 [英] Two arrays defining 2D coordinates, as array indices

查看:24
本文介绍了定义二维坐标的两个数组,作为数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组,称之为A.我还有另外两个二维数组,分别称为 ixiy.我想创建一个输出数组,其元素是 ixiy 提供的索引对处的 A 元素.我可以用如下循环来做到这一点:

I have a 2D array, call it A. I have two other 2D arrays, call them ix and iy. I would like to create an output array whose elements are the elements of A at the index pairs provided by ix and iy. I can do this with a loop as follows:

for i=1:nx
    for j=1:ny
        output(i,j) = A(ix(i,j),iy(i,j));
    end
end

如何在没有循环的情况下执行此操作?如果我执行 output = A(ix,iy),我会在 (ix)X(iy) 的整个范围内得到 A 的值.

How can I do this without the loop? If I do output = A(ix,iy), I get the value of A over the whole range of (ix)X(iy).

推荐答案

一种更快的方法是直接使用线性索引,而无需调用 SUB2IND:

A faster way is to use linear indexing directly without calling SUB2IND:

output = A( size(A,1)*(iy-1) + ix )

...将矩阵 A 视为一维数组(按列顺序)

... think of the matrix A as a 1D array (column-wise order)

这篇关于定义二维坐标的两个数组,作为数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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