I have 2 matrices:
A = lon, lat, pressure, temperature
B = lon, lat, depth
A size is bigger than B
I am trying to merge the data from A, B to obtain: (lon, lat, pressure, depth, Temperature)
My logic is:
  1. Identify values in which latA=latB AND lonA,lonB
  2. export both columns : lon, lat
  3. add pressure, depth and temperature values to the final matrix
The problem I face is that I want to
treat (lon, lat) as a one single column so that values do not split(?) like a row
and giving the different size in the two matrices, I want to tell matlab to compare one element (Alat1) with all the elements in B-lat column
I found I could use something like this:
lonMatch = ismember(A(:,1), B(:,1));
latMatch = ismember(A(:,2), B(:,2));
latlonMatch = lonMatch & latMatch; %of course the these 3 lines can be put into 1
Final matrix = A(latlonMatch, 3:end);
I am new to this kind of problem and I do not know how to approach it :(

 Akzeptierte Antwort

Johan
Johan am 20 Okt. 2021
Bearbeitet: Johan am 20 Okt. 2021
I'm not sure exactly what you mean by A is bigger than B, if you can give a simple example of A and B it would help in helping you :).
I have put a small example below with bogus values maybe this will help. All in all I do somehting very similar to what you proposed.
A = [1, 2, 50, 300; 1, 3, 65, 305]
A = 2×4
1 2 50 300 1 3 65 305
B = [1, 2, 150]
B = 1×3
1 2 150
list = and(abs(A(:,1)-B(:,1))<eps,abs(A(:,2)-B(:,2))<eps); % find matching values
C = A; %initiate final matrix
C(:,end+1) = NaN; %create a new column filled with NaN
C(list,end) = B(list,end) %Replace NaN by values of B in matching rows
C = 2×5
1 2 50 300 150 1 3 65 305 NaN

3 Kommentare

Oh I meant the sizes
A =40000x3
A(ix,:)= [7.7, 78.8, 350; 7.9, 81.3, 201]
B=311000x4
B(ix,:)= [8.2, 79.5, 200, -2.2; 7.9, 78.2, 0, 3.2]
Johan
Johan am 20 Okt. 2021
Bearbeitet: Johan am 20 Okt. 2021
Okay, then I guess the code I put before should work (swap A and B though has B as more row than A). You will not get the data of A that have no match in B though, I don't know if that is an issue. Also there are missing absolute value in the logical check (I edited my previous answer)
list = and(abs(A(:,1)-B(:,1))<eps,abs(A(:,2)-B(:,2))<eps)
Thanks a bunch, Johan :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Oceanography and Hydrology finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by