classify row numbers of a table
3 views (last 30 days)
Show older comments
Sanley Guerrier
on 8 Aug 2023
Commented: Sanley Guerrier
on 8 Aug 2023
Dear experts,
I would like to classify this data by rows, for example:
For every x and y point if the median of that row is T <= -12 classify it as "sunny area"
if the median of that row is -12< T <= -14 classify it as "sun-shade area"
finally, if the median of that row is T> -14 classify it as "shaded area"
I have no idea how to generate a matlab function to do that. Please, can you help with that?
Thank you!
0 Comments
Accepted Answer
the cyclist
on 8 Aug 2023
Already a couple different solid answers here, with slightly different methods, but I'm going to post it anyway. One difference with mine is that it keeps everything in a single table:
IMPORTANT NOTE: I did not correct your inequalities as @Voss did. But I believe the same thing he does, that you mixed them up.
% Read the table
tbl = readtable("T.xlsx",'VariableNamingRule','preserve');
% Calculate the median T from the columns, and add it as a column
tbl.medianT = median(tbl{:,["T0","T1","T2","T3","T4","T5","T6"]},2);
% Add an empty column for the sunniness
tbl = addvars(tbl,strings(height(tbl),1),'NewVariableNames','Sunniness');
% Fill in the sunniness for each median range
tbl{tbl.medianT <= -12, "Sunniness"} = "sunny area";
tbl{tbl.medianT > -12 & tbl.medianT <= -14,"Sunniness"} = "sun-shade area";
tbl{tbl.medianT > -14, "Sunniness"} = "shaded area";
disp(tbl)
10 Comments
the cyclist
on 8 Aug 2023
For any function you use, you need to understand the proper syntax for that function. If you don't know, then you need to read the documentation.
In the specific case of mean, the second argument is the dimension along which the mean is taken. (It is equivalent to how the median is done, not how the standard deviation is done).
More Answers (1)
Steven Lord
on 8 Aug 2023
Another way to do this, which could be simpler if you have more than 3 categories, is to use discretize.
data = randi([-16, -10], 10, 1); % 10-by-1 array of integers in range [-16, -10]
boundaries = [-Inf, -14, -12, Inf];
categories = ["shaded", "sun-shade", "sunny"];
D = discretize(data, boundaries, 'categorical', categories);
result = table(data, D)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!