根据前两个值合并两个数组

时间:2013-02-24 09:02:11

标签: matlab matrix merge delaunay

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

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

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

到目前为止,我已成功完成所有这些工作。 (感谢您在这一点上与我合作。)

我现在要做的是将两个矩阵(由插值数据组成)组合成一个矩阵。如果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

由此,我放弃了lat和long相同的重复数据,然后将两个数组newSSforY和newSSforX合并为一个数组。

2 个答案:

答案 0 :(得分:0)

所以我不是百分之百确定我理解你的问题,特别是关于'在不同列上附加信号强度的部分,如果它们不同我希望创建一个新行。'

我写了一个小脚本给出了你提供的两个矩阵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.我假设此脚本有效:长矩形坐标不能在矩阵内重复。意思是矩阵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),:)=[];
祝你好运!

答案 1 :(得分: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),:)=[];
相关问题