if else statement for tabular data classification
8 次查看(过去 30 天)
显示 更早的评论
I have data in a table that I would like to classify using an 'if else' statement.
My data is the differences in frequency (total_changes) at intervals on a curve. The data is currently in a 20x10 table and I am now looking to classify these frequency changes into the following categories:
big_rise: >=3000
small_rise: >=500 & <3000
constant: <500 & >-500
small_fall: <=-500 & >-3000
big_fall: >=-3000
From looking online, I have found this suggestion. It has not been working for me and has not given any error messages.
if total_changes<3000
fprintf('large_rise');
elseif total_changes (total_changes>500 & total_changes<3000)
fprintf('small_rise');
elseif total_changes (total_changes<500 & total_changes>-500)
fprintf('constant');
elseif total_changes (total_changes<-500 & total_changes>-3000)
fprintf('small_fall');
else
fprintf('large_fall')
end
I am pretty new to MATLAB so apologies if this is rudimentary stuff, but I would be extremely grateful for some help with this!
If any more information/screenshots are needed, please let me know.
Thanks
0 个评论
采纳的回答
Steven Lord
2020-7-1
discretize your data. Let's start off with some sample data. I'm using rng default so you receive the same results I did when I ran this example, so I know all five of your categories are represented.
rng default
x = randi([-5000 5000], 10, 1);
Define the categories and the edges for each.
cats = {'big fall', 'small fall', 'constant', 'small rise', 'big rise'};
edges = [-Inf -3000 -500 500 3000 Inf];
The interval [-Inf, -3000) is the 'big fall' category.
The interval [-3000, -500) is the 'small fall' category.
Now let's discretize. Note that by default MATLAB includes the left edge but not the right in each bin (except for the last bin, which includes both edes.) If you wanted to change that you'd add the 'IncludedEdge' option. See the documentation page for details on that option.
d = discretize(x, edges, 'categorical', cats);
Now let's see into which category each element of the data was binned.
t = table(x, d, 'VariableNames', {'Value', 'category'})
更多回答(1 个)
darova
2020-7-1
Try for loop
2 个评论
darova
2020-7-2
Sorry to misunderstand you. I mean just adding (i), not replacing
for i = 1:length(total_changes)
if total_changes(i)<3000
fprintf('large_rise');
elseif total_changes (total_changes(i)>500 & total_changes(i)<3000)
fprintf('small_rise');
elseif total_changes (total_changes(i)<500 & total_changes(i)>-500)
fprintf('constant');
elseif total_changes (total_changes(i)<-500 & total_changes(i)>-3000)
fprintf('small_fall');
else
fprintf('large_fall')
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!