if else statement for tabular data classification

조회 수: 8 (최근 30일)
RufusS
RufusS 2020년 6월 30일
댓글: RufusS 2020년 7월 7일
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

채택된 답변

Steven Lord
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
darova 2020년 7월 1일
Try for loop
  댓글 수: 2
RufusS
RufusS 2020년 7월 1일
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 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 CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by