2013-02-24 88 views
1

我將簡要介紹代碼背後的想法,然後深入研究我的問題。基於前兩個值合併兩個數組

我正在收集數據,其中第一個兩個coloumns將長和緯度座標,第三個列將是gps信號強度。

我將有兩個矩陣,每個矩陣對應不同的衛星。使用第一個矩陣的x,y座標,我使用第二個矩陣的TriScatteredInterp函數插值了信號強度(反之亦然)。我的想法是,我將爲各個地點創造可能的信號強度,並獲得某種超級delaunay三角測量。

到目前爲止,我已經完成了所有這些工作。 (感謝您在這一點上與我同在)。

我現在要做的是將兩個矩陣(包括插值數據)組合成一個矩陣。如果長和緯座標相同,即第一列和第二列中的前兩個值與第二列中的相同,我想將行的信號強度添加到第四列中。如果緯度和長度值不同,我希望在我的新矩陣中創建一個新行並添加數據。我已經編輯了這個問題,並在問題結尾附上了期望的答案。 ( - 爲了清晰起見,請仔細閱讀本節)

我絕對不知所措,希望您能給予幫助。我已經非常廣泛地搜索了有用的信息,但我一直不走運。

由於我有一些問題,這是最複雜的,我不確定是否發佈所有問題。我決定發佈一個,並會很感激,如果有人讓我知道提問更多問題的禮節;我是新來的。

謝謝你閱讀我的羅嗦問題,我很抱歉,我不能使它更簡潔。

謝謝你的任何幫助,你可以給。山姆

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組合成一個數組。

回答

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 

如果它看到,它有同樣長,郎座標它增加了第二個值到第四列。如果它們不匹配,就將其保留爲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),:)=[]; 

祝你好運!所以這裏

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),:)=[]; 
+0

謝謝elektrochose,這絕對是輝煌的。我目前離開我的辦公室,所以我無法檢查它是否會返回與我之後的回答相同的數據集(我編輯了它們)。我今晚會回來的。 – broomi 2013-02-25 09:44:44