Author Topic: help???matlab code to sort matrix  (Read 945 times)

Offline afrah

  • Newbie
  • *
  • Posts: 1
  • Matlab Forum
    • View Profile
help???matlab code to sort matrix
« on: May 06, 2010, 11:27:45 AM »
hello
i know there is  a function in matlab to sort a matrix, but i want to write this function ( code it) in matlab
 
 
A=[18  7
     6   15
     4   13];
result:
 
A=[4     7
     6   13
     18   15];
 
% The vector of numbers
disp ('INPUTS')
disp('Input the vector of numbers')
A=[18 7; 6 15; 4 13];
disp(A)
%% SOLUTION
% Number of entries, n
[m,n]=size(A);
%n=length(A);
disp('number of row');
disp(m);
disp('number of column');
disp(n);
for k=1:1:m-1
% making (n-1) passes
  for j=1:1:n
      % comparing each number with the next and swapping
    for i=1:1:n-1
      if A(i)>A(i+1);
          % temp is a variable where the numbers are kept
          % temporarily for the switch
          temp=A(i);
          A(i)=A(i+1);
          A(i+1)=temp;
         
      end % end if
    end  % end loop (i)
  end  % end loop (j)
   end  % end loop (k)
%% OUTPUT
disp('  ')
disp ('OUTPUT')
disp ('The ascending matrix is')
disp(A)
 
 
help ??? plzzzzzzzzz

Matlab and SimuLink Development Forum

help???matlab code to sort matrix
« on: May 06, 2010, 11:27:45 AM »