필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Miguel
Miguel 2023년 12월 14일
댓글: Miguel 2023년 12월 14일
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에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by