Swapping without using swap
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm trying to sort and swap elements, but I keep getting an error whenever I try to do so. Can someone please take a look at my code and tell me what I'm doing wrong? Thanks. Code:
Matrix = input('Please, input a matrix of any length: \n');
Order = input ('Please, press 1 for ascending order or 2 for descending order: \n');
if Order==1
for i=1:length(Matrix)
if i(i)>i(i+1)
storage=[i(i+1):i(i)];
end
end
end
1 Commento
Jan
il 13 Ott 2014
When ever you mention, that you get an error, post a complete copy of the error message. Please do not let us guess this important detail.
Risposte (3)
Image Analyst
il 13 Ott 2014
What do you mean by swap? The code is nonsense: i(i) <-- what is that? To sort, simply use the sort function
[sortedMatrix, fortOrder] = sort(matrix, 'ascend'); % or 'decend'
0 Commenti
Vedere anche
Categorie
Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!