Question

I will briefly explain the idea behind the code, and then delve into my problems.

I'm collecting data, where the first two coloumns will be long and lat co-ordinates and the third column will be gps signal strengths.

I will have two matrices, each corresponging to a different satellites. Using the x,y co-ordinates for the first matrix, I have interpolated the signal strengths using the TriScatteredInterp function of the second matrix (and vice versa). The idea being that I will create the probable signal strengths for the various locations and get some sort of super delaunay triangulation.

I've managed to get all of this done so far. (Thank you for bearing with me at this point).

What I want to do now, is combine the two matrices (that consist of the interpolated data) into one matrix. If the long and lat co-ordinates are the same, that is the first two values in column one and two, are the same as in the other matrix, I would like to add the signal strength to the row, into a fourth column. If the lat and long values are different I wish to create a new row in my new matrix and add the data. I have edited the question and attached the desired answer at the end of the question. (-Edited this section for clarity)

I am absolutely flummoxed and would appreciate any help that you can give. I have searched quite extensively for the useful information but I've been unlucky.

As I have a number of issues, this being the most complicated, I was unsure whether or not to post all the problems. I decided to post just one and would appreciate if someone let me know the ettiquette on asking more questions; I'm new here.

Thank you for reading my wordy question, I apologise that I couldn't make it more concise.

Thank you for any help that you're able to give. Sam

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. 

I have added the answer that I am after:

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

From this, I've discarded the duplicated data where the lat and longs are the same, and then merged to two arrays, newSSforY and newSSforX, into one array.

Was it helpful?

Solution

So I'm not a 100% sure I understood your question, specially the part about 'append the signal strengths along the various columns, if they are different I wish to create a new row.'

I wrote a little script that given your two matrices x and y you provided it spits out the following matrix:

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

If it saw that it had the same long,lang coordinates it added the second value to a fourth column. If they didn't match it just left it as 0. I made one assumption for this script to work: the long,lang coordinates cannot be repeated within a matrix. Meaning a matrix x cannot have two coordinates 1,5 and 1,5. I think this is a reasonable assumption.

If by append you mean add the signal intensities together, you can modify the code easily to achieve this. As far as the actual problem you're tackling I have no idea what you're doing but it sounds interesting. Let me know if it worked! (The code outputs some warnings, it's safe to ignore them I reckon). Here is the function:

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

Good luck!

OTHER TIPS

OK so here is how it would work on 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),:)=[];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top