在空间矩阵乘法之后找到索引位置

在空间矩阵乘法之后找到索引位置

本文介绍了在空间矩阵乘法之后找到索引位置. bsxfun已实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在复杂的矩阵乘法之后,我需要帮助找到一个矩阵的索引位置和两个向量,请耐心阅读我的第一篇,最后我的问题来了.

I need help finding some index-positions of a matrix and two vectors after a complicated matrix multiplication, please bear with me and read what I have first, my question comes at the end.

我有两个矩阵L1L2:

L1 = firstMatrix;
L2 = secondMatrix;

我需要计算L1中每个单个值与所有L2值的差异(列方式),同样,以列方式,如下所示:

I need to compute the difference (column-wise) of every single value from L1 with all the values of L2, again, in column-wise form, this is done as follows:

第一步

lib1 = bsxfun(@minus, L1(:,1)',L2(:,1));
lib1=lib1(:);
lib2 = bsxfun(@minus, L1(:,2)',L2(:,2));
lib2=lib2(:);
lib3 = bsxfun(@minus, L1(:,3)',L2(:,3));
lib3=lib3(:);

最后,我有了新的矩阵LBR:

At the end I have my new matrix LBR:

LBR = [lib1 lib2 lib3];

现在,在封闭域上,我有两个向量alphabeta,步长相同,在这种情况下,它们是相同的.

Now, I have two vectors alpha and beta -given on a closed domain with same step-size, in this case they are the same-.

alpha = 0:0.1:2;
beta = 0:0.1:2;

我现在需要计算张量积,我可以用两种方法来做到这一点:

I need now to compute the tensor-product, I can do it in two ways:

第二步

第一种方式:

alphat1 = kron(alpha,lib1);
alphat2 = kron(alpha,lib2);
alphat3 = kron(alpha,lib3);
T1 = [alphat1 alphat2 alphat3];
betat1 = kron(beta,lib1);
betat2 = kron(beta,lib2);
betat3 = kron(beta,lib3);
T2 = [betat1 betat2 betat3];

或者,第二种方式,这是通过matlab中的bsxfun来实现的:

Or, the second way, which is by means of the bsxfun from matlab:

val = bsxfun(@times,LBR,permute(alpha,[3 1 2]));
T = reshape(permute(val,[1 3 2]),size(val,1)*size(val,3),[]);

val2 = bsxfun(@times,LBR,permute(beta,[3 1 2]));
T2 = reshape(permute(val2,[1 3 2]),size(val2,1)*size(val2,3),[]);

我的问题:

我需要通过以下方式找到min-distance,首先,我当然具有三个常量:

I need to find the min-distance in the following way, first, I have of course three constants:

gama1 = value1;
gama2 = value2;
gama3 = value3;

min-distance的计算方式如下:

第三步

[d,p] = min(((T(:,1)-T2(:,1))-gama1).^2 + ((T(:,2)-T2(:,2))-gama2).^2 +
((T(:,3)-T2(:,3))-gama3).^2);
d = sqrt(d);

我非常需要满足该min-distance问题的L1L2alphabeta的索引位置.

I need, very much, the index positions of L1, L2, alpha and beta which are fulfilling this min-distance problem.

我尝试了以下操作:

第四步

[minindex_alongL2, minindex_alongL1, minindex_alongalpha, minindex_alongbeta] =
ind2sub([size(L2,1) size(L1,1) numel(alpha) numel(beta)],p);

但是它不起作用.非常感谢您能为我提供的所有帮助!

But it doesn't work. I would appreciate very much all the help you can provide me!

谢谢.

推荐答案

alphabeta分别获得TT2.然后,您将在TT2之间执行列式减法,而不是对T中的列的每个元素与T2中相同列号的所有元素进行减法.如果要执行后者,则最有可能需要彻底更改代码并摆脱串联,并扩展为多维数组.

alpha and beta gets T and T2 respectively. Then you are performing columnwise subtraction between T and T2 and not a subtraction for each element of a column in T against all elements of the same column number in T2. If you would like to perform the latter, you are most probably needed to change the codes from ground up and getting rid of concatenations and extending into multi-dimensional arrays.

继续执行代码中的内容,最多可以获得联合的alpha-beta索引,而不是alphabeta的单独索引.

Continuing with what you have in your codes, you can at most get joint alpha-beta indices and not separate indices for alpha and beta.

因此,您可以拥有类似-

Thus, you can have something like -

[minindex_alongL2, minindex_alongL1, minindex_alongalphabeta] = ind2sub([size(L2,1) size(L1,1) numel(alpha)],p)

假设您正在输入L1L2value1value2value3以及行向量alphabeta的值,请参见如果此代码对您有用-

Edit 1: Assuming you are inputting values for L1, L2, value1, value2, value3 and row vectors alpha and beta, see if this code works for you -

lib1 = bsxfun(@minus, L1(:,1)',L2(:,1)); %%//'
lib2 = bsxfun(@minus, L1(:,2)',L2(:,2)); %%//'
lib3 = bsxfun(@minus, L1(:,3)',L2(:,3)); %%//'

t1 = cat(3,lib1,lib2,lib3);
t2 = permute(alpha,[4 3 1 2]);
T = bsxfun(@times,t1,t2);

t22 = permute(beta,[4 3 1 2]);
T2 = bsxfun(@times,t1,t22);

gama1 = value1;
gama2 = value2;
gama3 = value3;

mat1 = bsxfun(@minus,T,permute(T2,[1 2 3 5 4]));
mat2 = bsxfun(@minus,mat1,permute([gama1;gama2;gama3],[3 2 1]));
mat2 = squeeze(sum(mat2,3));

[d,p] = min(mat2(:));
d = sqrt(d);

[minindex_alongL2, minindex_alongL1, minindex_alongalpha, minindex_alongbeta] = ind2sub([size(L2,1) size(L1,1) numel(alpha) numel(beta)],p)

这篇关于在空间矩阵乘法之后找到索引位置. bsxfun已实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:47