if else statement for tabular data classification

8 views (last 30 days)
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

Accepted Answer

Steven Lord
Steven Lord on 1 Jul 2020
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'})

More Answers (1)

darova
darova on 1 Jul 2020
Try for loop
  2 Comments
RufusS
RufusS on 1 Jul 2020
Hiya,
So I am trying the for loop like you suggested and adding in the variable i. I end up with only a number of 'large_rise' as they all seem to be classified the same.
Surely the numerical parameters need to be set? E.g. big_rise: >=3000
This is the edited code I wrote following your suggestion:
for i = 1:length(total_changes)
if total_changes(i)
fprintf('large_rise');
elseif total_changes (total_changes(i) & total_changes(i))
fprintf('small_rise');
elseif total_changes (total_changes(i) & total_changes(i))
fprintf('constant');
elseif total_changes (total_changes(i) & total_changes(i))
fprintf('small_fall');
else
fprintf('large_fall');
end
end
Thanks so much for your help!
darova
darova on 2 Jul 2020
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

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!