Create new variable from an existing variable but deleting some outliers
1 view (last 30 days)
Show older comments
Alejandra Uguet de Resayre
on 5 Mar 2022
Commented: Walter Roberson
on 7 Mar 2022
Hello!
I'm working with a NetCDF file and I have values of T,S,density,depth, chlorophyll and oxygen measured in time.
I plotted a vertical profile of oxygen with depth and I saw there were some outliers that need to be removed. So, I would like to create a new variable (oxycor) from the original oxygen variable but without those values. I would like to remove those values right in the corner with concentration of oxygen equal to zero.
I tried this and then plotting with 'glider.oxycor' but it says vectors must be the same length.
oxycorrection2=find(glider.oxy>150);
glider.oxycor=glider.oxy(oxycorrection2);
What am I doing wrong?
Thank you!!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/915424/image.png)
2 Comments
Walter Roberson
on 5 Mar 2022
Is glider a struct() or is it a table()
If it is a table, then you are trying to have the glider.oxy column be full length but the glider.oxycor column be shorter.
Accepted Answer
Walter Roberson
on 5 Mar 2022
mask = glider.oxy>150;
glider_subset = glider(mask,:);
2 Comments
Walter Roberson
on 7 Mar 2022
Struct case
mask = [glider.oxy] > 150;
glider_subset = glider(mask);
More Answers (1)
Steven Lord
on 5 Mar 2022
Remove all rows in the table that have an outlier value for the oxy variable. Here's a similar example using the sample patients data.
load patients
P = table(LastName, Age, Height, Weight);
P2 = P; % Make a backup copy before outlier removal
fprintf("P has %d rows before outlier removal\n", height(P))
head(P)
Let's say outliers are patients that are younger than 40.
tooYoung = P.Age < 40;
P(tooYoung, :) = [];
fprintf("P has %d rows after outlier removal\n", height(P))
head(P)
Note that several patients younger than 40 appeared in the table before outliers were removed, but afterwards the first patient in the resulting table was Johnson at 43 years old.
Now if you wanted to keep the data but not plot it, you could do that either by just using logical indexing or, if you want to compute with it more often, adding that as a new variable to the table.
P2.tooYoung = tooYoung;
head(P2)
plot(P2{tooYoung, "Height"}, P2{tooYoung, "Weight"}, 'o'); % or
figure
scatter(P2{P2.tooYoung, "Height"}, P2{P2.tooYoung, "Weight"})
0 Comments
See Also
Categories
Find more on NetCDF 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!