1

我将简要解释代码背后的想法,然后深入研究我的问题。

我正在收集数据,其中前两列是长坐标和纬度坐标,第三列是 gps 信号强度。

我将有两个矩阵,每个矩阵对应不同的卫星。使用第一个矩阵的 x,y 坐标,我使用第二个矩阵的 TriScatteredInterp 函数对信号强度进行了插值(反之亦然)。我的想法是我将为各个位置创建可能的信号强度并获得某种超级德劳内三角测量。

到目前为止,我已经设法完成了所有这些工作。(感谢您在这一点上容忍我)。

我现在要做的是将两个矩阵(由插值数据组成)组合成一个矩阵。如果 long 和 lat 坐标相同,即第一列和第二列中的前两个值与另一个矩阵中的相同,我想将信号强度添加到行中,添加到第四列中。如果 lat 和 long 值不同,我希望在我的新矩阵中创建一个新行并添加数据。我已经编辑了问题并在问题的末尾附上了所需的答案。(-为清楚起见,编辑了本节)

我非常困惑,并希望您能提供任何帮助。我已经广泛搜索了有用的信息,但我很不幸。

由于我有很多问题,这是最复杂的,我不确定是否要发布所有问题。我决定只发布一个,如果有人让我知道提出更多问题的礼仪,我将不胜感激;我是新来的。

感谢您阅读我冗长的问题,我很抱歉我无法使其更简洁。

感谢您提供的任何帮助。山姆

x = [1, 3, 5, 2, 4, 5, 3, 1, 2, 3, 4, 5;                %I wanteed to make an array that kind of made sense
1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 3, 4;                 %Using random values became kind of difficult
20, 40, 10, 50, 80, 60, 80, 40, 50, 50, 70, 20]';   

y = [0, 2, 4, 1, 2.5, 4, 2, 0, 1, 2, 3, 4;                 
2, 2, 2, 3, 4, 4, 4, 4, 5, 5, 4, 5;                 
10, 30, 20, 40, 70, 80, 90, 30, 60, 40, 80, 20]';

dt1 = DelaunayTri(x(:,1), x(:,2)); %This makes the dt for the x array
dt2 = DelaunayTri(y(:,1), y(:,2)); 

interp1 = TriScatteredInterp(x(:,1), x(:,2), x(:,3)); %I can use the dt to do this as below
interp2 = TriScatteredInterp(dt2, y(:,3));   %use the dt array here like so

newValuesforY = interp1(y(:,1), y(:,2)); %This line uses the interpretation function of the DT for x, to predict values at the y co-ords that I enter.
yNew = [y newSSforY];

newSSforX = interp2(x(:,1), x(:,2));
xNew = [x newSSforX];
xNew(:,[3,4])=xNew(:,[4,3]); %I swap these around for clarity

%I now wish to merge the two, as mentioned in the above post. 

我添加了我想要的答案:

0   2   10  NaN
0   4   30  NaN
1   1   NaN 20
1   3   40  40
1   5   60  NaN
2   2   30  50
2   4   90  50
2   5   40  NaN
2.5 4   70  57.5
3   1   NaN 40
3   3   52.5    80
3   4   80  65
3   5   30  50
4   2   20  80
4   3   50  70
4   4   80  46.66666667
4   5   20  NaN
5   1   NaN 10
5   3   NaN 60
5   4   NaN 20

由此,我丢弃了经纬度相同的重复数据,然后将两个数组newSSforY和newSSforX合并为一个数组。

4

2 回答 2

0

所以我不是 100% 确定我理解了你的问题,特别是关于“沿着各个列附加信号强度,如果它们不同,我希望创建一个新行”的部分。

我写了一个小脚本,给定您提供的两个矩阵 x 和 y,它会输出以下矩阵:

1.00000    1.00000   20.00000    0.00000
3.00000    1.00000   40.00000    0.00000
5.00000    1.00000   10.00000    0.00000
2.00000    2.00000   50.00000   30.00000
4.00000    2.00000   80.00000   20.00000
5.00000    3.00000   60.00000    0.00000
3.00000    3.00000   80.00000    0.00000
1.00000    3.00000   40.00000   40.00000
2.00000    4.00000   50.00000   90.00000
3.00000    5.00000   50.00000    0.00000
4.00000    3.00000   70.00000    0.00000
5.00000    4.00000   20.00000    0.00000
0.00000    2.00000   10.00000    0.00000
2.50000    4.00000   70.00000    0.00000
4.00000    4.00000   80.00000    0.00000
0.00000    4.00000   30.00000    0.00000
1.00000    5.00000   60.00000    0.00000
2.00000    5.00000   40.00000    0.00000
3.00000    4.00000   80.00000    0.00000
4.00000    5.00000   20.00000    0.00000

如果它看到它具有相同的 long,lang 坐标,它会将第二个值添加到第四列。如果它们不匹配,则将其保留为 0。我为该脚本工作做了一个假设:long,lang 坐标不能在矩阵中重复。这意味着矩阵 x 不能有两个坐标 1,5 和 1,5。我认为这是一个合理的假设。

如果附加意味着将信号强度加在一起,则可以轻松修改代码以实现此目的。至于您要解决的实际问题,我不知道您在做什么,但听起来很有趣。让我知道它是否有效!(代码输出一些警告,我认为忽略它们是安全的)。这是功能:

function temp=n_merge(A,B)
%Assumption 1:long, lang coordinates do not repeat within matrix A
%Assumption 2:long, lang coordinates do not repeat within matrix B

%Creating output matrix (over-allocating size)
temp=[A;zeros(size(B,1),3)];
temp=[temp zeros(size(temp,1),1)];

%Initializing variables for for-loop
counter=1;
sp=size(A,1);%starting point

for i=1:size(B,1)
b=sum(B(i,1:2)==A(:,1:2),2);                                             

if isempty(find(b==2))%if the long,lang coordinates are not in matrix A
temp(sp+counter,1:3)=B(i,1:3);
counter=counter+1;

else %if the long,lang coordinates are in matrix B
temp(find(b==2),4)=B(i,3);
end
end

%Removing all rows with 0's on them
temp(all(temp==0,2),:)=[];

祝你好运!

于 2013-02-24T19:48:22.510 回答
0

好的,这就是它在 MATLAB 上的工作方式:

function temp=n_merge(A,B)
%Assumption 1:long, lang coordinates do not repeat within matrix A
%Assumption 2:long, lang coordinates do not repeat within matrix B

%Creating output matrix (over-allocating size)
temp=[A;zeros(size(B,1),3)];
temp=[temp zeros(size(temp,1),1)];

%Initializing variables for for-loop
counter=1;
sp=size(A,1);%starting point

for i=1:size(B,1)
for j=1:size(A,1)
b(j,:)=B(i,1:2)==A(j,1:2);
end
b=sum(b,2);

if isempty(find(b==2))%if the long,lang coordinates are not in matrix A
temp(sp+counter,1:3)=B(i,1:3);
counter=counter+1;

else %if the long,lang coordinates are in matrix B
temp(find(b==2),4)=B(i,3);
end
end

%Removing all rows with 0's on them
temp(all(temp==0,2),:)=[];
于 2013-02-24T20:01:42.693 回答