How to streamline this code to average certain rows?
Mostrar comentarios más antiguos
I have a three row column data: lon lat and Z. What I want to do is to round longitude and latitude to 2 digits. For example, 65.3796 will be rounded 65.38. Then the program will sort the data based on longitude and latitude, and average any rows with identical longitudes and latitudes. For example,
The below matrix:
65.39 23.56 10.5
66.70 25.36 6.7
66.70 25.36 7.8
Will become:
65.39 23.56 10.5
66.70 25.36 7.25
Here is my code. The problem is that I have several dozens of millions of data. It will take forever to complete. Anyone could help me create a more streamlined code, so that the program will be much faster?
I really appreciate your help!
clear all
close all
clc
load data/data_3row % D: lon lat Z
lon = D(:,1);
lat = D(:,2);
for i=1:length(lon)
lon(i,1) = round(lon(i,1)*100)/100;
lat(i,1) = round(lat(i,1)*100)/100;
end
D2 = [lon, lat, D(:,3)];
D3 = sortrows(D2, [1:2]);
D3 = [D3; 0 0 0];
[m n] = size(D3);
Ind1 = D3(1,1);
Ind2 = D3(1,2);
row = D3(1,:);
D4 = [];
n4 = 0;
for i=2:m
if D3(i,1)==Ind1 & D3(i,2)==Ind2
row = [row; D3(i,:)];
else
n4 = n4+1;
D4(n4,:) = [row(1,1), row(1,2), nanmean(row(:,3))];
Ind1 = D3(i,1);
Ind2 = D3(i,2);
row = D3(i,:);
end
end
save('data/data_3row_avg.mat', 'D4');
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!