For Loop or filter to select equal data in a data file ?

1 view (last 30 days)
Hello
I have a dat file with the following data, and through them, I need to select the same values of Latitude and then select longitude values that belong to these. After that, I also need agemodel values that match the values mentioned above. I also have to find the nearest value of 20 to apply in a simple mathematical equation (eg Depth (linha10) * 20/agemodel (10)).
I thought about using a for loop or even a filter, but I'm a little confused. Someone can give me a hint?
Thank you! Nuno Simões
Attach file:
name Latitude Longitude Depth[m] agemodel pa1 20.291700 -18.261700 7.543 102.000 pa2 20.291700 -18.261700 8.211 105.700 pa3 20.291700 -18.261700 9.222 116.500 pa4 20.291700 -18.261700 9.923 128.300 pa5 20.291700 -18.261700 9.972 134.000 pa6 20.291700 -18.261700 10.128 137.300 pa7 20.749200 -18.580800 0.020 0.080 pa8 20.749200 -18.580800 0.040 0.160 pa9 20.749200 -18.580800 0.050 0.200 pa10 20.749200 -18.580800 0.060 0.240 pa11 20.749200 -18.580800 0.080 0.320 pa12 20.749200 -18.580800 0.100 0.390 pa13 20.749200 -18.580800 0.100 0.400 pa14 20.749200 -18.580800 0.120 0.470 pa15 20.749200 -18.580800 0.140 0.550 pa16 20.749200 -18.580800 0.150 0.600 ... 40 000 data

Accepted Answer

Star Strider
Star Strider on 19 Apr 2014
I do not completely understand what you want.
I called your variable array ‘LLDA’, and managed to get this far:
LLDA = {'pa1' 20.291700 -18.261700 7.543 102.000 ; 'pa2' 20.291700 -18.261700 8.211 105.700 ; 'pa3' 20.291700 -18.261700 9.222 116.500 ; 'pa4' 20.291700 -18.261700 9.923 128.300 ; 'pa5' 20.291700 -18.261700 9.972 134.000 ; 'pa6' 20.291700 -18.261700 10.128 137.300 ; 'pa7' 20.749200 -18.580800 0.020 0.080 ; 'pa8' 20.749200 -18.580800 0.040 0.160 ; 'pa9' 20.749200 -18.580800 0.050 0.200 ; 'pa10' 20.749200 -18.580800 0.060 0.240 ; 'pa11' 20.749200 -18.580800 0.080 0.320 ; 'pa12' 20.749200 -18.580800 0.100 0.390 ; 'pa13' 20.749200 -18.580800 0.100 0.400 ; 'pa14' 20.749200 -18.580800 0.120 0.470 ; 'pa15' 20.749200 -18.580800 0.140 0.550 ; 'pa16' 20.749200 -18.580800 0.150 0.600};
LLDAn = LLDA(:,2:5);
LLDAm = cell2mat(LLDAn)
[LatUq, iL, iU] = unique(LLDAm(:,1));
for k1 = 1:size(LatUq,1)
LLU(k1) = {[LLDAm(LLDAm(:,1)==LatUq(k1), 1:2)]};
end
LLum = cell2mat(LLU(1))
LLum = cell2mat(LLU(2))
Experiment with it, and if it does what you want, expand on it. It is not the most efficient code I ever wrote, but it does isolate the longitudes for the same latitudes in the cell array LLU.
  2 Comments
Nuno Simões
Nuno Simões on 21 Apr 2014
Hi,
thanks for the help
I've done the following script:
"fname1 = uigetfile({'*.txt;*.dat;*.tab','Data Files (*.txt,*.dat,*.tab)'; '*.*','All Files (*.*)'},'Ficheiro de dados da Pangaea');
[name,latitude,longitude,depth,agemodel] = textread(fname1,'%s %f %f %f %f','headerlines',1);
LLDA = [latitude,longitude,depth,agemodel] % Matriz
LLDAn = LLDA(:,1:4) %LLDA(:,2:5)
LLDAm = LLDAn% cell2mat(LLDAn)
[LatUq, iL, iU] = unique(LLDAm(:,1))
[LonUq, iL, iU] = unique(LLDAm(:,2))
for k1 = 1:size(LatUq,1) % selecionar em latitudes iguais
LLU(k1) = {[LLDAm(LLDAm(:,1)==LatUq(k1), 1:4)]};
end
for k2 = 1:size(LonUq,1) % selecionar em longitudes iguais
LLU(k2) = {[LLDAm(LLDAm(:,2)==LonUq(k2), 1:4)]};
end
for k3 = 1:size(LonUq,1) % ver dados mais próximos de 20 e selecionar so a linha do dado correspondente
LLU(k3) = {[LLDAm(LLDAm(:,2)==LonUq(k3), 1:4)]} % ao mais proximo
if LLDAm(:,4) > 20
LLU(k3) = {[LLDAm((LLDAm(:,4)./20) == LonUq(k3), 1:4)]}
else LLDAm(:,4) < 20
LLU(k3) = {[LLDAm(min(20./LLDAm(:,4))== LonUq(k3), 1:4)]}
end
end
for k4 = 1:size(LonUq,1) % adicionar uma coluna com o valor da profundidade a 20ka
LLU(k4) = {[LLDAm((LLDAm(:,3)*20)/LLDAm(:,4) == LonUq(k4), 1:4)]}
end
for k5 = 1:size(LonUq) % dados de sáida .. por latitude e longitude
LLUm = cell2mat(LLU(k5))
end
"
But I can not do the 3 and 4 "for loop". "After that, I also need agemodel that values match the values mentioned above (Latitude e longitude). I also have to find the nearest value of 20 to apply in a simple mathematical equation (eg Depth (linha10) * 20/agemodel (10) ). "
At the end will give the following result, for example: Latitude, Longitude, Depth (calculated with the formula), Agemodel (20)
Star Strider
Star Strider on 21 Apr 2014
I am more than a bit lost. Since agemodel is the 4th column, I suggest you get and store them when you get your latitude and longitude information.
I do not understand ‘(linha10) * 20/agemodel (10)’. I do not see linha10 as a variable anywhere. The agemodel (10) term seems to me to refer to the 10th element in the agemodel vector ( 4th column of your data array ).

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 19 Apr 2014
Hi Nuno,
I think that you are on the right track with the for loop. If the names of each record is pa1,pa2,pa3,.. which seems to be the case from your example, I would read in the data and create a single (numeric only) matrix with the following columns in this order: latitude, longitude, depth, agemodel, id where the id for the ith element is 'i' rather than the 'pai'. So you have a Nx5 matrix, called mtxData for each of the N elements read in from the file.
Call * mtxDataSorted=sorrows(mtxData);* which will sort all the rows according the first column, then the second, etc. Since the first column is latitude, then all your identical latitude records will be grouped together. Now if you start at the first row of mtxDataSorted, i=1 say, then iterate through each subsequent element comparing the current latitude to the previous one. If the latitude has changed at index j, then all elements from 1,2,…,j-1 have the same latitude. And so now you have all the required data for the first latitude: the longitudes (column 2 of mtxDataSorted), the depth (column 3 of mtxDataSorted), and the age model data (column 4 of mtxDataSorted). You can apply your equation to this set of data and then continue iterating. If the latitude changes at iteration k, then all elements from j,j+1,…,k-1 have the same latitude.
Hope that this helps.
Geoff
  2 Comments
Nuno Simões
Nuno Simões on 19 Apr 2014
Hi Geoff,
Thank you very much for your help. Yeah, quite understand the part the matrices, sortRows, etc ...
Only I am not able to implement the following: Compare the current latitude with previous one and see then all elements from 1,2, ..., j-1 have the same latitude? I thought to use the diff command ... Sorry for the inconvenience
Again thank you very much
Nuno Simões
Geoff Hayes
Geoff Hayes on 19 Apr 2014
Hi Nuno,
So assume you have your matrix with all rows sorted as described above, and assume that there are N rows in the matrix (mtxDataSorted). Save the first latitude as lat1=mtxDataSorted(1,1) since latitudes are in the first column of the matrix. Now iterate as follows:
k=1;
for i=2:N
if abs(mtxDataSorted(i,1)-lat1) > eps
% since the latitude at i is somewhat different than
% lat1, then assume that all latitudes prior to i are
% identical: so all rows from k to i-1 have the same
% latitude
% do work on group
% reset vars
k = i;
lat1 = mtxDataSorted(i,1);
end
end
You will need one final check outside of the for loop to handle the last group of common latitudes.
Geoff

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!