I need to know a command to find errors in data file

3 次查看(过去 30 天)
I have a program which analyzes ticket sales and I have everything but one of the requirements is to find the errors on the data file (the data file is "WDWtixdata1.txt")(The errors are numbers grearest than 10 like 11,12,13, etc and numbers lower than 1 like 0,-1, etc.
Which command can I use to find the errors? The one I used tells me hat theres 8 errors but the right answer is 101 so I am doing somthing wrong.
I would appreciate the help.
% This Program analyzes ticket sales data for an event at Walt Disney World
% Load data from the file
data = load('WDWtixdata1.txt');
% Filter out errors (tickets greater than 10 days or less than 1 day)
valid_data = data(data >= 1 & data <= 10);
% Define ticket prices
one_day_price = 122.00;
two_to_three_day_price = 101.67;
four_to_seven_day_price = 67.50;
eight_to_ten_day_price = 48.34;
% Calculate total number of tickets sold
total_tickets_sold = numel(valid_data);
% Calculate tickets sold in each category
one_day_tickets = sum(valid_data == 1);
two_to_three_day_tickets = sum(valid_data >= 2 & valid_data <= 3);
four_to_seven_day_tickets = sum(valid_data >= 4 & valid_data <= 7);
eight_to_ten_day_tickets = sum(valid_data >= 8 & valid_data <= 10);
% Calculate percentages
one_day_percentage = (one_day_tickets / total_tickets_sold) * 100;
two_to_three_day_percentage = (two_to_three_day_tickets / total_tickets_sold) * 100;
four_to_seven_day_percentage = (four_to_seven_day_tickets / total_tickets_sold) * 100;
eight_to_ten_day_percentage = (eight_to_ten_day_tickets / total_tickets_sold) * 100;
% Calculate total revenue
total_revenue = (one_day_tickets * one_day_price) + (two_to_three_day_tickets * two_to_three_day_price * valid_data(1)) + (four_to_seven_day_tickets * four_to_seven_day_price * valid_data(1)) + (eight_to_ten_day_tickets * eight_to_ten_day_price * valid_data(1));
% Identify errors
total_errors = (load('WDWtixdata1.txt') <= 1 & data >= 10);
% Display results
disp(['WDW sold a total of ' num2str(total_tickets_sold) ' tickets.']);
disp(['WDW sold ' num2str(one_day_tickets) ' one-day tickets, which is ' num2str(one_day_percentage) '% of all tickets sold.']);
disp(['WDW sold ' num2str(two_to_three_day_tickets) ' two-to-three-day tickets, which is ' num2str(two_to_three_day_percentage) '% of all tickets sold.']);
disp(['WDW sold ' num2str(four_to_seven_day_tickets) ' four-to-seven-day tickets, which is ' num2str(four_to_seven_day_percentage) '% of all tickets sold.']);
disp(['WDW sold ' num2str(eight_to_ten_day_tickets) ' eight-to-ten-day tickets, which is ' num2str(eight_to_ten_day_percentage) '% of all tickets sold.']);
disp(['Walt Disney World took in a total of $' num2str(total_revenue)]);
disp(['The data file WDWtixdata1.txt had a total of ' numel(total_errors) ' errors.']);
  2 个评论
Steven Lord
Steven Lord 2023-12-14
% Calculate tickets sold in each category
one_day_tickets = sum(valid_data == 1);
two_to_three_day_tickets = sum(valid_data >= 2 & valid_data <= 3);
four_to_seven_day_tickets = sum(valid_data >= 4 & valid_data <= 7);
eight_to_ten_day_tickets = sum(valid_data >= 8 & valid_data <= 10);
% Calculate percentages
one_day_percentage = (one_day_tickets / total_tickets_sold) * 100;
two_to_three_day_percentage = (two_to_three_day_tickets / total_tickets_sold) * 100;
four_to_seven_day_percentage = (four_to_seven_day_tickets / total_tickets_sold) * 100;
eight_to_ten_day_percentage = (eight_to_ten_day_tickets / total_tickets_sold) * 100;
For these sections (and perhaps for the calculation of total revenue) you may want to look at the histcounts, discretize, and/or groupsummary functions. You wouldn't want to handle with individually named variables a case where there were dozens of categories of tickets, would you?
Miguel
Miguel 2023-12-14
Thats right Steven, thanks for the tip! I will take a look but now I need to make this program run first. If you can help me I would appreciate it. Thank you

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2023-12-14
编辑:the cyclist 2023-12-14
In this line
% Identify errors
total_errors = (load('WDWtixdata1.txt') <= 1 & data >= 10);
I think you intend something more like
% Identify errors
total_errors = (data <= 1) | (data >= 10); % Notice that I changed your AND condition to an OR
  1 个评论
Miguel
Miguel 2023-12-14
Thank you! That might work but if I change that line I also need to modify the last line in the display. How should I modify it?
This line
disp(['The data file WDWtixdata1.txt had a total of ' num2str(total_errors) ' errors.']);

请先登录,再进行评论。

更多回答(1 个)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023-12-14
You stated greater or but your code statement is greater and equal: >= 1 & data <= 10
That should be: valid_data = data(data > 1 | data < 10);
You can also check using: sum(data > 1 | data < 10)
  1 个评论
Miguel
Miguel 2023-12-14
I just change those two lines and it says
Error in FinalProgram (line 50)
disp(['The data file WDWtixdata1.txt had a total of ' num2str(total_errors) ' errors.']);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by