unknown code- can anyone help me with what this code does?
Mostrar comentarios más antiguos
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)
Respuesta aceptada
Más respuestas (1)
John D'Errico
el 15 de Mzo. de 2020
Editada: John D'Errico
el 15 de Mzo. de 2020
0 votos
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 comentario
Josh Williams
el 15 de Mzo. de 2020
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!