From: PauliePorsche on
In addition to sorting an array, i want it so that every time i call the array in another part of the m file, it references the sorted array; not the original

For example if i have an array which looks like this:

RandomArray =

2
4
6
1
2
4
21
2
2
3

If I input RandomArray(10) - the last element within the array, it should spit out 3

after sorting


sortrows(RandomArray)

ans =

1
2
2
2
2
3
4
4
6
21



RandomArray(10) will still spit out 3, i want it to return 21 or whatever the last element after sorting


How can I do this?
From: TideMan on
On Jul 12, 4:11 pm, PauliePorsche <Bhopal...(a)gmail.com> wrote:
> In addition to sorting an array, i want it so that every time i call the array in another part of the m file, it references the sorted array; not the original
>
> For example if i have an array which looks like this:
>
> RandomArray =
>
>      2
>      4
>      6
>      1
>      2
>      4
>     21
>      2
>      2
>      3
>
> If I input RandomArray(10) - the last element within the array, it should spit out 3
>
> after sorting
>
> sortrows(RandomArray)
>
> ans =
>
>      1
>      2
>      2
>      2
>      2
>      3
>      4
>      4
>      6
>     21
>
> RandomArray(10) will still spit out 3, i want it to return 21 or whatever the last element after sorting
>
> How can I do this?

SortedArray=sortrows(RandomArray);
SortedArray(10)
From: Image Analyst on
PauliePorsche:
Just assign the output of sortrows to some new variable.

RandomArray =[2; 4; 6; 1; 2; 4; 21; 2; 2; 3]
RandomArray(10) % Displays 3;
sortedArray = sortrows(RandomArray)
sortedArray(10) % Displays 21

% If you want, you can stick it back in RandomArray
RandomArray = sortedArray;
From: kk KKsingh on
"Image Analyst" <imageanalyst(a)mailinator.com> wrote in message <i1e77c$ddk$1(a)fred.mathworks.com>...
> PauliePorsche:
> Just assign the output of sortrows to some new variable.
>
> RandomArray =[2; 4; 6; 1; 2; 4; 21; 2; 2; 3]
> RandomArray(10) % Displays 3;
> sortedArray = sortrows(RandomArray)
> sortedArray(10) % Displays 21
>
> % If you want, you can stick it back in RandomArray
> RandomArray = sortedArray;

Just GIve the output some variable name

like

x=sorted(Randomarray);
x(10)=answer
and its 10th element will be your answer