简体   繁体   English

将数组中值的特定索引与数组MatLab的大小相关的方程式

[英]Equation relating the specific index of a value in an array and the size of the array MatLab

I'm trying to come up with an equation that relates the index of a value within a 3D array, to the index of the same array but reshaped into a column vector. 我正在尝试提出一个方程,该方程将3D数组中的值的索引与同一数组的索引相关联,但将其整形为列向量。

Consider the following. 考虑以下。

A = randi([1,10],3,2,2);
A2 = reshape(A,3*2*2,1);

A and A2 have the same number of elements, but the arrangement of the elements is different for each array. AA2具有相同数量的元素,但每个数组的元素排列均不同。 If I lay out a possible example for A and A2 here it is clear geometrically how each index lines up. 如果我在这里为AA2列出一个可能的示例,则可以很清楚地看出每个索引的排列方式。

A(:,:,1) = [9 10; 10 7;  2  1]
A(:,:,2) = [3 10;  6 2; 10 10]

A2 = [9; 10; 2; 10; 7; 1; 3; 6; 10; 10; 2; 10]

Let's say n=1:1:3*2*2 , this is an array that is the same length as A2 and numbers each of the elements. 假设n=1:1:3*2*2 ,这是一个数组,其长度与A2相同,并为每个元素编号。 The value of A(2,2,2)=2 and has indices [i,j,k]=[2,2,2] . A(2,2,2)=2并具有索引[i,j,k]=[2,2,2] I would like to have an equation relating i, j, k, and n. 我想有一个与i,j,k和n有关的方程式。

I've looked into the built-in functions ind2sub and sub2ind but it seems that I inadvertently shaped my i, j, and k coordinates (which correspond with real x, y, and z points) differently than how MatLab does. 我已经研究了内置函数ind2subsub2ind但是似乎我无意中对我的i,j和k坐标(与真实的x,y和z点相对应)的形状进行了调整,与MatLab所做的不同。 This makes it difficult for me to change everything now, and is why I need an equation. 这使我现在很难更改所有内容,这就是为什么我需要方程式的原因。

The conversion between 3D index and linear (1D) index is given by: 3D索引和线性(1D)索引之间的转换由下式给出:

n=i+(j-1)*M + (k-1)*M*N

The reverse can be obtained recursively as: 反向可以递归获得:

k = floor((n-1)/(M*N)) +1 
n = n - (k-1)*M*N
j = floor((n-1)/M) + 1
i = n - (j-1)*M 

I haven't tested it, but I think it will give you what you are expeccting. 我还没有测试过,但是我认为它将给您带来什么。

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

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