How to find duplicated values and calculate the mean of them?
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a data set of points that consist latitudes, longitudes and respectively velocities. I need to find the same points (same latitudes and longitudes), but need to take the average of their velocities. So my new data set will consist no duplicates but instead averaged velocity values of those duplicants. But I dont know how can I find the duplicated values and how to merge them with non duplicated ones.
You can find my code and data here;
clear all;
a = load('all_velocities.txt');
A = [a(:,1) a(:,2)]; % latitude and longitudes
k=1;
for i = 1:length(A)
[U, ia] = unique(A, 'rows'); %unique values
DATA(k,:) = [U(ia,1) U(ia,2) a(ia,3) a(ia,4)]; % new data set with unique lat and lon and their velocities
k=k+1;
end
So far I ended up with this error message:
Index in position 1 exceeds array bounds (must not exceed 385).
Error in duplicate (line 9)
DATA(k,:) = [U(ia,1) U(ia,2) a(ia,3) a(ia,4)];
Also I need to calculate the average velocity of those duplicated ones and merge them with data.
댓글 수: 0
답변 (1개)
Stephen23
2020년 2월 21일
편집: Stephen23
2020년 2월 21일
Here is one solution:
>> M = dlmread('all_velocities.txt');
>> [~,~,X] = unique(M(:,1:2),'rows');
>> F = @(x) mean(M(x,:),1);
>> Data = arrayfun(F,1:max(X),'UniformOutput',false));
>> Data = vertcat(Data{:});
Another option would be to use a table:
댓글 수: 3
Stephen23
2020년 2월 21일
@Sevil Cansu Yildirim: your comment shows some unrelated code which uses different data files to the one you provided in your question. I have no idea how your comment relates to your question or to my answer.
참고 항목
카테고리
Help Center 및 File Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!