%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Ryan Davis % Metrics 2 - HW#1 % September 1st, 2010 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % clear, clc % get data data = importdata('datayx.csv', ',') % since the filename lists y first, then assume y is in first column Y = data(:,1); X = data(:,2); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % A. Sort X from smallest to largest %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % can get bonus points for faster search (more efficient) % hint: what if split into 2, do loops for each % use mean perhaps % Now, we sort using bubble sort % Number of entries, n X_sort_bubble = X; n=length(X); % since we compare the current number (i) to the next number i+1, % then we have i=1,...,n-1 so that i+1=2,...n % since we only have n numbers (length of vector X) for j=1:1:n-1 for i=1:1:n-1 % note that the loops are nested, since we must compare each number to % every other number in the sort, and swapping the items if they are % not in the right order. if X_sort_bubble(i)>X_sort_bubble(i+1); temp=X_sort_bubble(i); X_sort_bubble(i)=X_sort_bubble(i+1); X_sort_bubble(i+1)=temp; end end end % display sorted list X_sort_bubble % check results using built in function X_sort = sort(X,'ascend'); if X_sort_bubble == X_sort disp('X matches!') else disp('Error: X does not match!') end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % B. Sort Y from largest to smallest %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Just use the same procedure as above, flopping the inequality sign Y_sort_bubble = Y; n=length(Y); for j=1:1:n-1 for i=1:1:n-1 if Y_sort_bubble(i)