hi this is my homework ı want to sort matrix A from smallest to largest.But I m stuck here .Can you help me ? Ps:I cant use sort,min,max or other functions

1 view (last 30 days)
clc;clear;
A=[24.3 24.8 24.6 24.0 22.3 21.0 20.5 20.7 18.8 17.6 15.5 17.5 19.0 19.8 21.4]
B=[]
for i=1:length(A)
ek=100;
for i=1:length(A)
if A(i)<ek
ek=A(1,i)
end
end
B(1,i)=A(i)
A(i)=[]
end
B
  3 Comments
Mustafa Yasin Erdogan
Mustafa Yasin Erdogan on 4 Apr 2022
ı dont know how many sorting methods exist in matlab.But selection sort method is suitable for me i think.Thank you for your answer.
per isakson
per isakson on 5 Apr 2022
Edited: per isakson on 5 Apr 2022
Google for "selection sort pseudocode". I think Selection sort pseudocode is an appropriate read. I'm sure that, regardless of the definition of functions, loops and if-statemens and logical operators are allowed.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 5 Apr 2022
It is not possible to solve that problem in MATLAB without using any functions. For an explaination see https://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions?s_tid=srchtitle

Walter Roberson
Walter Roberson on 5 Apr 2022
clc;clear;
A=[24.3 24.8 24.6 24.0 22.3 21.0 20.5 20.7 18.8 17.6 15.5 17.5 19.0 19.8 21.4]
A = 1×15
24.3000 24.8000 24.6000 24.0000 22.3000 21.0000 20.5000 20.7000 18.8000 17.6000 15.5000 17.5000 19.0000 19.8000 21.4000
B=[]
B = []
for i=1:length(A)
ek=100;
j_at_min = 0;
for j=1:length(A)
if A(j)<ek
ek=A(1,j);
j_at_min = j;
end
end
B(1,i)=A(j_at_min)
A(j_at_min)=[]
end
B = 15.5000
A = 1×14
24.3000 24.8000 24.6000 24.0000 22.3000 21.0000 20.5000 20.7000 18.8000 17.6000 17.5000 19.0000 19.8000 21.4000
B = 1×2
15.5000 17.5000
A = 1×13
24.3000 24.8000 24.6000 24.0000 22.3000 21.0000 20.5000 20.7000 18.8000 17.6000 19.0000 19.8000 21.4000
B = 1×3
15.5000 17.5000 17.6000
A = 1×12
24.3000 24.8000 24.6000 24.0000 22.3000 21.0000 20.5000 20.7000 18.8000 19.0000 19.8000 21.4000
B = 1×4
15.5000 17.5000 17.6000 18.8000
A = 1×11
24.3000 24.8000 24.6000 24.0000 22.3000 21.0000 20.5000 20.7000 19.0000 19.8000 21.4000
B = 1×5
15.5000 17.5000 17.6000 18.8000 19.0000
A = 1×10
24.3000 24.8000 24.6000 24.0000 22.3000 21.0000 20.5000 20.7000 19.8000 21.4000
B = 1×6
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000
A = 1×9
24.3000 24.8000 24.6000 24.0000 22.3000 21.0000 20.5000 20.7000 21.4000
B = 1×7
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000
A = 1×8
24.3000 24.8000 24.6000 24.0000 22.3000 21.0000 20.7000 21.4000
B = 1×8
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000 20.7000
A = 1×7
24.3000 24.8000 24.6000 24.0000 22.3000 21.0000 21.4000
B = 1×9
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000 20.7000 21.0000
A = 1×6
24.3000 24.8000 24.6000 24.0000 22.3000 21.4000
B = 1×10
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000 20.7000 21.0000 21.4000
A = 1×5
24.3000 24.8000 24.6000 24.0000 22.3000
B = 1×11
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000 20.7000 21.0000 21.4000 22.3000
A = 1×4
24.3000 24.8000 24.6000 24.0000
B = 1×12
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000 20.7000 21.0000 21.4000 22.3000 24.0000
A = 1×3
24.3000 24.8000 24.6000
B = 1×13
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000 20.7000 21.0000 21.4000 22.3000 24.0000 24.3000
A = 1×2
24.8000 24.6000
B = 1×14
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000 20.7000 21.0000 21.4000 22.3000 24.0000 24.3000 24.6000
A = 24.8000
B = 1×15
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000 20.7000 21.0000 21.4000 22.3000 24.0000 24.3000 24.6000 24.8000
A = 1×0 empty double row vector
B
B = 1×15
15.5000 17.5000 17.6000 18.8000 19.0000 19.8000 20.5000 20.7000 21.0000 21.4000 22.3000 24.0000 24.3000 24.6000 24.8000

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!