【发布时间】:2014-06-25 14:38:21
【问题描述】:
我的代码需要帮助。该代码用于求平方距离问题的minumin。我通过示例提供我的代码,我相信这将是解释我需要的最简单的方法。
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必须这样形成!这就是我在标题中写 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;
给出满足这个 min 问题的距离 (d) 和位置 (p)。
我的问题:
我需要找出A and B 的哪些组合给了我这个p 值,换句话说,我需要来自'A,B' 的索引,它给了我这个最佳 C1,C2,C3: p>
C1 = bsxfun(@minus, A(?,1)',B(?,1));
C2 = bsxfun(@minus, A(?,2)',B(?,2));
C3 = bsxfun(@minus, A(?,3)',B(?,3));
?是我需要的索引位置,这里是矩阵A的索引位置和B的索引位置。
手工计算我有以下图解:
我知道:
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 位。这个索引位置可以追溯到:
[13-2 20-3 16-10]
A(4,:) - B(1,:)
我需要一个代码来帮助我从 A 和 B 中找到这个索引
提前致谢!
PS。我在 ODE 的参数估计问题中使用代码。
【问题讨论】:
-
这很令人困惑,从标量和向量之间的距离开始。向量没有位置,只有大小和方向。你的意思是你想找到一个点和存储在一个向量中的一组点之间的最短距离吗?然后你谈谈矩阵
A和B。鉴于点和向量之间距离的某种模糊定义,很难理解A和B代表什么。第三,您应该修复示例代码中z中的错误。
标签: arrays matlab matrix indexing minimum