Question
Please help me fix this MATLAB Code to sort the vector in ASCENDING order and NOT descending order: (Please write a clear explanation in the
Please help me fix this MATLAB Code to sort the vector in ASCENDING order and NOT descending order:
(Please write a clear explanation in the code of what each line does and what was changed to aid my understanding, thanks)
% MATLAB Script that reads a vector, Sort the vector in Descending order and prints
clc; clear;
% Reading input vector from user ipVec = input("Enter a vector: ");
% Sorting vector in descending order using Bubble Sort Logic % Set size to length of the array passed size = length(ipVec); % Outer loop runs from 1 to size for outer_loop=1:size % Inner loop runs from 1 to size-1 for inner_loop=1:size-1 % Comparing elements if ipVec(inner_loop) < ipVec(inner_loop + 1) % Swapping elements temp = ipVec(inner_loop + 1); ipVec(inner_loop + 1) = ipVec(inner_loop); ipVec(inner_loop) = temp; end %end if statement end %end for inner_loop end %end outer_loop
% Printing vector after sorting fprintf(" After Sorting in Descending Order: "); disp(ipVec);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started