Matlab算法转换坐标 [英] Matlab algorithm to transform coordinates

查看:137
本文介绍了Matlab算法转换坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在Matlab中有一个大小为MxNxK的矩阵A.假设我在列向量B中变换了A.我想要一个代码,给定A中第i,j,h个元素的坐标,将B中相同元素的坐标k拆分.

Suppose I have a matrix A in Matlab of size MxNxK. Suppose I transform A in a column vector B. I want a code that, given the coordinates of the i,j,hth element in A, splits the coordinate k of the same element in B.

我写了一个代码,可以完成我想要的事情.由于我正在尽力优化代码(坐标操作的更改应被拒绝数百万次),因此我想知道您是否可以想到更有效的解决方案.

I have written a code that does what I want. As I'm trying to optimise my code as much as I can (the change of coordinate operation should be repated millions of times), I would like to know whether you can think of more efficient solutions.

让我用一个例子更好地解释.

Let me explain better with an example.

clear all

A(:,:,1)=[1 2 3; 4 5 6];
A(:,:,2)=[7 8 9; 10 11 12];
A(:,:,3)=[13 14 15; 16 17 18];
A(:,:,4)=[19 20 21; 22 23 24];

B=reshape(A,[24,1]);

假设changecoord(i,j,h)是给出位置的函数 B

Suppose changecoord(i,j,h) is the function that gives the position of A(i,j,h) in B

然后,该算法应拆分

%changecoord(1,1,1)=1
%changecoord(2,1,1)=2
%changecoord(1,2,1)=3
%changecoord(2,2,1)=4
%changecoord(1,3,1)=5
%changecoord(2,3,1)=6

%changecoord(1,1,2)=7
%changecoord(2,1,2)=8
%changecoord(1,2,2)=9
%changecoord(2,2,2)=10
%changecoord(1,3,2)=11
%changecoord(2,3,2)=12

%changecoord(1,1,3)=13
%changecoord(2,1,3)=14
%changecoord(1,2,3)=15
%changecoord(2,2,3)=16
%changecoord(1,3,3)=17
%changecoord(2,3,3)=18

%changecoord(1,1,4)=19
%changecoord(2,1,4)=20
%changecoord(1,2,4)=21
%changecoord(2,2,4)=22
%changecoord(1,3,4)=23
%changecoord(2,3,4)=24

这是我写的算法

function k=changecoord(s1, s2,i,j,h)
%s1=size(A,1)
%s2=size(A,2)
p1=(s1*s2)*h-(s1*s2)+1:1:s1*s2*h;
p2=p1(s2*j-s2+1:s2*j);
k=p2(i);
end

推荐答案

您的函数将效率不高,因为它会创建一个(可能)较大的数组,然后在其中找到一个值:

You function will not be efficient, because it creates a (potentially) large array, then finds one value in it:

function k=changecoord(s1, s2,i,j,h)
%s1=size(A,1)
%s2=size(A,2)
p1=(s1*s2)*h-(s1*s2)+1:1:s1*s2*h;
p2=p1(s2*j-s2+1:s2*j);
k=p2(i);
end

您可以看到,如果jh为1,则k==i.这对于所有i值(范围为1:s1)均有效.内存中的下一个元素再次具有i=1,但具有j=2.在这里,k=s1+1.即,对于j=2的所有元素,均k=s1+i.当j=3变为k=2*s1+i等.因此在2D索引中:

You can see that if j and h are 1, then k==i. This is valid for all i values, which range 1:s1. The next element in memory has i=1 again, but j=2. Here, k=s1+1. That is, k=s1+i for all elements where j=2. When j=3 this becomes k=2*s1+i, etc. Thus in 2D indexing:

k = i + (j-1)*s1;

对于下一个索引,我们可以完全相同.当i=s1j=s2时,我们已经用尽了第一个平面,并开始在第二个平面中找到元素(h=2).因此,将k增加s1*s2即可访问此平面中的所有元素.因此:

We can do exactly the same for the next index. When i=s1 and j=s2, we have exhausted the first plane, and start finding elements in the second plane (h=2). k will thus be increased by s1*s2 to access all the elements in this plane. Thus:

k = i + (j-1)*s1 + (h-1)*s1*s2;

可以通过以下方式简化

k = i + ( j-1 + (h-1)*s2 ) * s1;

尽管 sub2ind 执行相同的计算该功能比手动写出计算要多一些开销.

This same computation is performed by sub2ind, though that function has a little bit more overhead than writing out the computation by hand.

这篇关于Matlab算法转换坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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