Hello, I have two questions: How do I determine if there is a direct route between x and y in this code? How do I determine the one-stop routes in this code? Thank you
1 次查看(过去 30 天)
显示 更早的评论
Hello, I'm a novice at MatLab and I need some assitance with a few lines of code. Thank you for your help.
Notice: To clarify, I'm not asking you to work out the entire project or anything like that. I will post the prompt the teacher has given us and the example just incase y'all need that information. I will also be posting my code. Sorry if this comes off as rude, just don't want to send the wrong idea.
Problems I need assitance with:
How do I determine if there is a direct route between x and y?
How do I determine the one-stop routes?
These will be all the way at the bottom of the code, the comments label them
Prompt:
Table 2 Example from Prompt:
My code:
myproj('City.xlsx')
function myproj(fname)
miles = xlsread(fname);
A = (miles > 0);
B = A.^2;
fprintf('Cities to choose from: ');
disp(1:length(A));
x = input('Departure city: ');
y = input('Destination city: ');
fprintf('\n');
if x == y
disp('You entered the same Departure city and Destination city!\n')
else
% Display number of routes
total_routes = A(x,y) + B(x,y);
str1 = sprintf('Total numbers of routes: %d', total_routes);
disp(str1);
% Start a table on screen
xlswrite('City.xlsx',x);
xlswrite('City.xlsx',y);
disp('Stop Distance');
% Set up column headings in Excel
str2 = sprintf('Routes between City %d and City %d', x, y);
xlswrite(fname, {str2}, 2, 'A1');
xlswrite(fname, {str1}, 2, 'A2');
xlswrite(fname, {'Stop'}, 2, 'A3');
xlswrite(fname, {'Distance'}, 2, 'B3');
% Prepare vectors for writing to Excel
xlswrite(fname, A, 'A1');
routes(1:total_routes, 1) = {' '}; % Route name (Direct or City name)
distances(1:total_routes, 1) = 0; % Mile of each route
num_route = 0; % Keep track of row number in matrix “routes” and “distances”
% Determine if there is a direct route between Cities x and y
if(A(2,i) == 1)
dirroute = miles(2,i);
end
% Display on screen and also add the route to “routes” and “distances” for Excel
disp(['City',num2str(1i),' ',num2str(dep(i)),'+',num2str(des(i)),'=',num2str(routes(j,1)),' Miles'])
% Determine one-stop routes between x and y
% Display on screen and also add the route to “routes” and “distances” for Excel
% Writing results to Excel
end
end
4 个评论
John D'Errico
2022-5-4
There should be a button on the top right that says edit, with a little icon that looks like a pencil.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!