Define data list with a determine data distribution

2 views (last 30 days)
Hi all,
I would like to use a min data distribution in my code. For example: I define each data positions are 500 or more. So when the first point difference is less than 700 with the second point, the second point will be deleted and the third point become the second one and so on. The results that I want like this:
mindist = 700; %minimum treshold for Data1
Data1 = [67106; 67732; 117467; 118467; 118967];
Data2 = [759.6421; 23.3729; 232.5324; 23.3729; 231.5324];
And I want the results to become:
Data1 = [67106; 117467; 118467];
Data2 = [759.6421; 232.5324; 23.3729];
Thank you, hope to hear your reply soon

Answers (1)

Joe Vinciguerra
Joe Vinciguerra on 29 Jun 2023
% Generate some random data that's similar to yours
Data1 = sort(randi(1e4,[10,1])); % making sure the data is sorted
Data2 = randi(1e4, [10,1])/10;
% Display the starting data
[Data1, Data2]
ans = 10×2
1.0e+03 * 0.1480 0.3258 0.5420 0.8107 1.2350 0.4113 2.2130 0.9183 2.4090 0.4879 5.1600 0.1724 6.4910 0.2494 7.7980 0.4882 9.7600 0.1659 9.9870 0.5157
% Set the mindist
mindist = 700;
% Loop through all the data, starting with the first number
i = 1;
% While we are not at the end of the data set ...
while i < numel(Data1)
% ...and while the distance to the next item is too small
while (i < numel(Data1)) && (Data1(i+1) - Data1(i) < mindist)
% Remove the next item from the data set
Data1(i+1) = [];
Data2(i+1) = [];
end
% Go to the next item in the data set
i = i + 1;
end
% Display the results
[Data1, Data2]
ans = 7×2
1.0e+03 * 0.1480 0.3258 1.2350 0.4113 2.2130 0.9183 5.1600 0.1724 6.4910 0.2494 7.7980 0.4882 9.7600 0.1659

Community Treasure Hunt

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

Start Hunting!