
Matlab Homework Solution on Epetition Structures (while and for loops)
- 8th Jul, 2022
- 15:12 PM
%% ____________________ %% INITIALIZATION %Initialising vectors and variables as below v = [2 -9 -3 6.5] v1=v v2=v l=length(v) descending=[0 0 0 0] ascending =[0 0 0 0] i=1 iteration=zeros(1,4) index=zeros(1,4) D_vector=zeros(1,4) %% ____________________ %?LCULATIONS ?scending %Use while loop for sorting, using flowchart while(i<=l) descending(i)=max(v1) iteration(1,i) = i-1 index(1,i) = i D_vector(1,i)= descending(i) v1(find(v1==max(v1)))=min(v1)-1 i=i+1 disp(i) end %Create table for descending d_table = table(iteration.',index.',D_vector.') %Ascending %Repeat above for ascending table i=1 while(i<=l) ascending(i)=min(v2) iteration(1,i) = i-1 index(1,i) = i D_vector(1,i)= ascending(i) v2(find(v2==min(v2)))=max(v2)+1 i=i+1 end a_table = table(iteration.',index.',D_vector.') d_table.Properties.VariableNames(1:3) = {'Iteration','Index','Descending_Vector'} a_table.Properties.VariableNames(1:3) = {'Iteration','Index','ascending_Vector'} %% ____________________ %% FORMATTED TEXT %Exporting to pdf file %Ascending vector b=a_table{1,:} c=a_table{2,:} d=a_table{3,:} e=a_table{4,:} fig = figure('Name','Ascenting table'); t = uitable(fig,'Data',[ b;c;d;e]); set(t, 'ColumnName', {'Iteration', 'Index', 'Ascending number'}); saveas(gcf,'Ascending','pdf') ?scending vector ?=d_table{1,:} ?=d_table{2,:} ?=d_table{3,:} ?=d_table{4,:} %fig = figure('Name','Decenting table'); %t = uitable(fig,'Data',[ b1;c1;d1;e1]); %set(t, 'ColumnName', {'Iteration', 'Index', 'Descending number'}); %saveas(gcf,'Descending','pdf') %% ____________________ %?ADEMIC INTEGRITY STATEMENT % I have not used source code obtained from any other unauthorized % source, either modified or unmodified. I have not provided ?cess to my code to anyone in any way. The script I am % submitting is my own original work.