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("\nAfter Sorting in Descending Order: "); disp(ipVec);
Please help me fix this MATLAB Code to sort the
(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("\nAfter Sorting in Descending Order: ");
disp(ipVec);
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images