classify row numbers of a table
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
the cyclist
2023-8-8
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 个评论
the cyclist
2023-8-8
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).
更多回答(1 个)
Steven Lord
2023-8-8
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)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!