Reduce rows of data based on increment size

11 vues (au cours des 30 derniers jours)
Brian Robinson
Brian Robinson le 19 Sep 2020
Commenté : Ameer Hamza le 20 Sep 2020
I have a set of data with the depth, z (m) 0 until the maximum length. At the moment there is a total of 951 rows due to the small increment size of z. For analysis purposes, I only require the increment size to be 0.25 m.
So basically what I want to do is the following (pseudocode)
  • Import the excel file into matlab.
  • inc_size = 0.25
  • Iterating through the rows, deltaZ_total = deltaZ(i)
  • While deltaZ_total < (inc_size)
  • deltaZ_total = deltaZ_total + deltaZ(i+1) % while the total of the increments is less than inc_size add them together
  • Create a new row containing deltaZ_total and delete all the previous rows
  • Skip a row % skip a row so the new increments are not being added to the previous
  • Repeat the process until the end of the column
Could I please have some help translating this idea into matlab code.
Thanks very much,
Brian
  4 commentaires
Image Analyst
Image Analyst le 19 Sep 2020
Not really, or very little. Are we talking about a 3-D dataset, like a CT or MRI volumetric image? Or just simply a 1-D situation where we have some number of elements in Z and the value of each element of Z is the depth into or above some material?
Please attach your data.
So, does Z go from say 20 to 10000 in 951 elements, but without a constant delta between each pair of elements in Z. Like it might be 0.1 between one pair of elements but 1.4 between a different pair of elements? And you want to resample that range 20-10000 with uniform spacing of 0.25. So the number of elements would be (max(z) - min(z)) / 0.25? Then you can just use linspace():
minValue = min(Z(:));
maxValue = max(Z(:));
numElements = (maxValue - minValue) / 0.25;
zUniform = linspace(minValue, maxValue, numElements);
Brian Robinson
Brian Robinson le 20 Sep 2020
Not really, or very little. Are we talking about a 3-D dataset, like a CT or MRI volumetric image? Or just simply a 1-D situation where we have some number of elements in Z and the value of each element of Z is the depth into or above some material?
It is simply a 1-d dataset where z is the depth below ground surface.
Please attach your data.
Please see attached.
So, does Z go from say 20 to 10000 in 951 elements, but without a constant delta between each pair of elements in Z. Like it might be 0.1 between one pair of elements but 1.4 between a different pair of elements? And you want to resample that range 20-10000 with uniform spacing of 0.25. So the number of elements would be (max(z) - min(z)) / 0.25? Then you can just use linspace():
As you will see from the data, z is not in constant increments but varies widely. In some cases delta_Z is > 0.25 so in this case the row should be left. Using linspace command can create a uniformly spaced vector, but that won't help in this case as the function will not know which rows of the table to consolidate.

Connectez-vous pour commenter.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 19 Sep 2020
Instead of deleting the rows, I suggest using interp1() to get the required output
z = .. % 951x1 vector
x = .. % 951x1 vector of data points
z_new = 0:0.25:max(z); % new z vector have increments of 0.25
x_new = interp1(z, x, z_new); % x_new are data points corresponding to z_new.
  6 commentaires
Brian Robinson
Brian Robinson le 20 Sep 2020
I tried this code, but I don't think its suitable because data in the z column is changed and also the data in the other column of the table is changed during the interp1 operation. I need the original z values to be preserved (other than when the increment size is too small deleting the entire rows of the table) and all the data from the corresponding columns to be preserved.
I have made that delta_Z column for ease in cleaning the table. So for example, the 'cleaned' table would look the same for the first 9 rows until we get to a delta_Z of 0.0488. The entire rows where delta_Z = 0.0488, 0.0518, 0.0518, 0.0488 can be deleted as these increments total less than 0.25. So the 'cleaned' table would go from delta_Z = 0.2560 to delta_Z = 0.0518 in row 10. And the other columns such as stroke for row 10 (= 1.5506) are preserved.
I hope this clarifies.
Ameer Hamza
Ameer Hamza le 20 Sep 2020
I think you want something like this
load pile_data.mat
purdueUS52MODIFIEDS2(1,:) = []; % first row is NaN, removing it
data = table2array(purdueUS52MODIFIEDS2);
count = 1;
while size(data, 1) ~= count
if data(count+1, 20) < data(count, 20)+0.25
data(count+1, :) = [];
else
count = count + 1;
end
end
data(2:end, 21) = diff(data(:, 20));
table_new = array2table(data, 'VariableNames', purdueUS52MODIFIEDS2.Properties.VariableNames);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by