Swapping without using swap

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 件のコメント

Jan
Jan 2014 年 10 月 13 日
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.

サインインしてコメントする。

回答 (3 件)

Image Analyst
Image Analyst 2014 年 10 月 13 日

0 投票

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'
Jeffrey
Jeffrey 2014 年 10 月 13 日

0 投票

I can't use the sort function on this assignment. I have to create my own way of sorting without using the sort command. Can this be done using a for loop and conditional statements? If so, how would I go about doing that for ascending matrices and descending matrices?

1 件のコメント

Jan
Jan 2014 年 10 月 13 日
Is this a comment to Image Analyst's answer? Then please use the comment section. He asked to clarify what "i(i)" should mean.

サインインしてコメントする。

Jeffrey
Jeffrey 2014 年 10 月 13 日

0 投票

What I mean by "swap" is the following: Say you want the user to give you an array. Let's say the array is [3,5,4]. I have to find a way, without using the sort function, to display the array as:
[3,4,5] or [5,4,3].

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2014 年 10 月 12 日

コメント済み:

Jan
2014 年 10 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by