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!