找到一个组成矩阵的指数位置 [英] finding index-positions of a composed-matrix

查看:148
本文介绍了找到一个组成矩阵的指数位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用我的code帮助。在code被用于查找的 minumin 的平方距离的问题。我通过一个实例,提供我的code,我相信这将是解释什么,我需要最简单的方法。

I need help with my code. The code is used to find the minumin of a square-distance problem. I am providing my code through an example, I believe this will be the easiest way to explain what I need.

clear all
clc
x=10.8; % is a fixed value
y=34; % is a fixed value
z=12; % is a fixed value
A = [11 14 1; 5 8 18; 10 8 19; 13 20 16]; % a (4x3) matrix
B = [2 3 10; 6 15 16; 7 3 15; 14 14 19]; % a (4x3) matrix

我创建了一个新的矩阵 C 这在本下列方式组成:

C1 = bsxfun(@minus, A(:,1)',B(:,1)); 
C1=C1(:); % this is the first column of the new matrix C
C2 = bsxfun(@minus, A(:,2)',B(:,2));
C2=C2(:); % this is the second column of the new matrix C
C3 = bsxfun(@minus, A(:,3)',B(:,3));
C3=C3(:); % this is the third column of the new matrix C
C = [C1 C2 C3]; % the new matrix C of size (16x3)

C 在这种方式中形成!而这也正是我的意思是,当我在我的标题写了组成的矩阵

C has to be formed in this way! And this is what I meant when I wrote in my title a composed-matrix

然后:

[d,p] = min((C(:,1)-x).^2 + (C(:,2)-y).^2 + (C(:,3)-z).^2);
d = sqrt(d);
outputs:
d = 18.0289;
p = 13;

让我满足该的的问题的距离(d)和位置(P)。

Gives me the distance (d) and position (p) which satisfies this min problem.

我的问题:
我需要找到哪些 A和B 的组合给了我这个 P 价值,换句话说,我需要的指数从'A,B'这给了我这样的优化 C1,C2,C3

MY PROBLEM: I need to find which combinations of A and B has given my this p value, in other words I need the index from ´A,B´ which gives me this optimal C1,C2,C3:

C1 = bsxfun(@minus, A(?,1)',B(?,1));
C2 = bsxfun(@minus, A(?,2)',B(?,2));
C3 = bsxfun(@minus, A(?,3)',B(?,3));

是索引位置,我需要,在这种情况下,矩阵A的指数位置和B的索引位置。

The ? is the index position I need, in this case the index position of the matrix A and the index position of B.

手工计算我有如下图所示:

Calculated by hand I have the following illustration:

我知道:

C = [9    11    -9
 5    -1   -15
 4    11   -14
-3     0   -18
 3     5     8
-1    -7     2
-2     5     3
-9    -6    -1
 8     5     9
 4    -7     3
 3     5     4
-4    -6     0
11    17     6
 7     5     0
 6    17     1
-1     6    -3]

我知道,我的最佳指数的在13位给出。该指数持仓又回到了:

And I know that my optimal index is given in the position 13th. This index positions goes back to:

[13-2 20-3 16-10]

这是 A(4 :) - B(1:)

我需要一个code,可以帮助我找到A和B这个指标

I need a code which can help me to find this indexes from A and B

在此先感谢!

PS。我使用的常微分方程参数估计问题code。

PS. I am using the code in parameter estimation problems of ODEs.

推荐答案

第一种情况:矢量矩阵情况下

subvals = bsxfun(@minus,A,[x y z])
[distance,index] = min(sqrt(sum(subvals.^2,2)))

情况二:两个矩阵的情况下

subvals = bsxfun(@minus,A,permute(B,[3 2 1]));
[distances,indices] = min(sqrt(sum(subvals.^2,2)),[],3)

测试的第二种情况:

%%// Get some random data into A and B
A = randi(20,8,3)
B = randi(20,4,3)

%%// Just to test out out code for correctness, 
%%// let us make any one one row of B, say 3rd row equal to 
%%// any one row of A, say the 6th row -
B(3,:) = A(6,:)

%%// Use the earlier code
subvals = bsxfun(@minus,A,permute(B,[3 2 1]));
[distances,indices] = min(sqrt(sum(subvals.^2,2)),[],3)

%%// Get the minimum row index for A and B
[~,min_rowA] = min(distances)
min_rowB = indices(min_rowA)

验证

min_rowA =
     6

min_rowB =
     3

修改1 [响应张贴在问题简单的例子]:

标题说,你有兴趣在寻找两个矩阵的差异,然后找到一个向量[X Y Z]这之间的最短距离。所以,我希望这是你所需要的 -

The title says you are interested in finding the difference of two matrices and then find the shortest distance between it to a vector [x y z]. So I am hoping this is what you need -

x=10.8;
y=34;
z=12;
A = [11 14 1; 5 8 18; 10 8 19; 13 20 16];
B = [2 3 10; 6 15 16; 7 3 15; 14 14 19];

C = A -B; %%// Distance of two vectors as posted in title
subvals = bsxfun(@minus,C,[x y z])
[distance,index] = min(sqrt(sum(subvals.^2,2)))

输出

distance =
   31.0780

index =
     3

编辑2:当你这样做之后 -

Edit 2: After you have done this -

[d,p] = min((C(:,1)-x).^2 + (C(:,2)-y).^2 + (C(:,3)-z).^2);

如果你想找到A和B对应的指数,你可以做到这一点 -

If you are looking to find the corresponding indices of A and B , you may do this -

[minindex_alongB,minindex_alongA] = ind2sub(size(A),p)

这篇关于找到一个组成矩阵的指数位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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