I need to know a command to find errors in data file
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Miguel
el 14 de Dic. de 2023
Comentada: Miguel
el 14 de Dic. de 2023
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 comentarios
Steven Lord
el 14 de Dic. de 2023
% 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?
Respuesta aceptada
the cyclist
el 14 de Dic. de 2023
Editada: the cyclist
el 14 de Dic. de 2023
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
Más respuestas (1)
Sulaymon Eshkabilov
el 14 de Dic. de 2023
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)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!