Help with MATLAB, creating a sorting function

In summary, the function msort helps sort numbers in ascending and descending order. To put numbers in descending order, the user must include 'd' in the input. The function uses a while loop to check for sorted numbers and a for loop to compare and swap the numbers. The number of swaps is also counted and can be shown as an output. The function can also handle multiple input arguments using varargin and output arguments using varargout.
  • #1
NosajW
11
0
This is what I had originally

function out = msort(x)
%This function helps sort numbers in ascending and descending order.
%To put in descending order, user must include 'd' in the input.

y = length(x);
sorted = 0;
n = 0;

while ~sorted
sorted = 1;
for i = 1:y-1
if x(i) > x(i+1)
n = n + 1;
t = x(i);
x(i) = x(i+1);
x(i+1) = t;
sorted = 0;
end
end
end


% while ~sorted
% sorted = 1;
% for i = 1:y-1
% if x(i) < x(i+1)
% n = n + 1
% t = x(i);
% x(i) = x(i+1);
% x(i+1) = t;
% sorted = 0;
% end
% end
% end;

out = x;

The program works if i set x = to a set of numbers and run msort(x), it won't work if I do msort(10,9,5,4). It will work if I do msort([10,9,5,4]). I realized I have to use varargout and varargin which leads me to change the first line of the program to:

function [varargout] = msort(varargin)

but if I change that I am not sure how I would change the rest of the code with all the x's, and I'm also not sure how I would be able to show the number of swaps with the sorted numbers like

[b,n] = msort(10,9,5,4)

b = 4,5,9,10
n = 6 (number of swaps)

I didn't do the descending part of the program yet. Please help! Thanks
 
Physics news on Phys.org
  • #2
There should be indentations there but it's not showing for some reason..
 
  • #3
function [varargout] = msort(varargin)
%This function helps sort numbers in ascending and descending order.
%To put in descending order, user must include 'd' in the input.
x = cell2mat(varargin);
y = nargin;
sorted = 0;
n = 0;
while ~sorted
sorted = 1;
for i = 1:y-1
if x(i) > x(i+1)
n = n + 1;
t = x(i);
x(i) = x(i+1);
x(i+1) = t;
sorted = 0;
end
end
end

% else
%
% while ~sorted
% sorted = 1;
% for i = 1:y-1
% if x(i) < x(i+1)
% n = n + 1
% t = x(i);
% x(i) = x(i+1);
% x(i+1) = t;
% sorted = 0;
% end
% end
% end
% end

if nargout > 2
disp('Too many output arguments.')
return
end

for i = 1:nargout
varargout{1} = x;
varargout{2} = n;
end

i got up to this now... I'm not sure how to allow the user to do descending

msort(1,2,3,4,5,'d')

? Error using ==> cell2mat
All contents of the input cell array must be of the same data type.

Error in ==> msort at 13
x = cell2mat(varargin);
 
  • #4
NosajW said:
function [varargout] = msort(varargin)
Code:
%This function helps sort numbers in ascending and descending order.
%To put in descending order, user must include 'd' in the input.



x = cell2mat(varargin);
y = nargin;
sorted = 0;
n = 0;


    
    while ~sorted
        sorted = 1;
        for i = 1:y-1
            if x(i) > x(i+1)
                n = n + 1;
                t = x(i);
                x(i) = x(i+1);
                x(i+1) = t;
                sorted = 0;
            end
        end
    end

% else
%     
%     while ~sorted
%         sorted = 1;
%         for i = 1:y-1
%             if x(i) < x(i+1)
%                 n = n + 1
%                 t = x(i);
%                 x(i) = x(i+1);
%                 x(i+1) = t;
%                 sorted = 0;
%             end
%         end
%     end
% end

if nargout > 2
    disp('Too many output arguments.')
    return
end

for i = 1:nargout
    varargout{1} = x;
    varargout{2} = n;
end

i got up to this now... I'm not sure how to allow the user to do descending

msort(1,2,3,4,5,'d')

? Error using ==> cell2mat
All contents of the input cell array must be of the same data type.

Error in ==> msort at 13
x = cell2mat(varargin);

Use the
Code:
 tag for fixed width spacing.  It will make things much nicer for everyone.
 

FAQ: Help with MATLAB, creating a sorting function

What is MATLAB?

MATLAB is a high-level programming language and interactive environment commonly used in scientific and engineering applications. It allows users to perform data analysis, visualization, and computation tasks.

How can I create a sorting function in MATLAB?

To create a sorting function in MATLAB, you can use the built-in function "sort" which allows you to sort arrays in ascending or descending order. You can also customize the sorting criteria by providing your own comparison function.

How do I use the sorting function in my code?

To use the sorting function in your code, you first need to define the input array that you want to sort. Then, you can use the "sort" function and specify the desired sorting options. Finally, you can store the sorted array in a new variable or use it directly in your code.

Can I sort different data types using the sorting function?

Yes, the "sort" function in MATLAB can handle different data types such as numeric arrays, character arrays, and cell arrays. However, the sorting criteria may vary depending on the data type.

Are there any alternative ways to sort data in MATLAB?

Yes, there are other functions in MATLAB that can be used for sorting data such as "sortrows" for sorting rows of a matrix and "sortrows" for sorting columns of a matrix. Additionally, you can also use loops and conditional statements to create your own sorting algorithm.

Similar threads

Replies
8
Views
861
Replies
1
Views
2K
Replies
2
Views
3K
Replies
5
Views
2K
Replies
4
Views
3K
Replies
8
Views
2K
Replies
8
Views
2K
Back
Top