unknown code- can anyone help me with what this code does?
Mostra commenti meno recenti
clear
r=rand(15,1)
for i=1:15
for ii=1:15
if (r(i)>r(ii))
temp=r(i);
r(i)=r(ii);
r(ii)=temp;
end
end
end
disp(r)
Risposta accettata
Più risposte (1)
John D'Errico
il 15 Mar 2020
Modificato: John D'Errico
il 15 Mar 2020
0 voti
If I had to guess, it looks like a poorly written attempt at a sorting code, to sort a vector of numbers in decreasing order. My guess is it was written by a student, as the swapping scheme seems a bit kludgy, and the overall code looks as if written by a novice. The clear in front is also the common act of a student.
Why do I say poorly written?
- Because it is fully uncommented. Uncommented code is of no value at all. (see below)
- Because it is an inefficient way to perform a sort, making direct comparisons between all elements of the array with all other elements, and even worse, even with themselves too.
The problem is, code written just as code, with no comments as to what it does, and taken completely out of context is typically just a random string of characters. It has as much value as the output from a thousand monkees, banging away at typewriters.
1 Commento
Josh Williams
il 15 Mar 2020
Categorie
Scopri di più su Shifting and Sorting Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!