Sort a vector in matlab

1 view (last 30 days)
Camilo  Corredor
Camilo Corredor on 27 Aug 2015
Commented: Camilo Corredor on 31 Aug 2015
Somebody can help me? From the routine in Matlab I get a vector whose length can be a number between 0 and 24. The content of this vector are numbers between 0 and 24. However, I have to sort this vector with the next conditions
1. The content must be equal to index of the sorted vector. 2. If in the content of the vector, a number doesn't exist must be equal to NaN
For example
Index Content
  • 1 10
  • 2 11
  • 3 12
  • 4 15
  • 5 16
  • 6 17
  • 7 18
  • 8 19
  • 9 20
  • 10 22
  • 11 24
In the left side is index vector and in right side the content The sorted vector that I hope to get is:
  • 1 2
  • 2 2
  • 3 3
  • 4 4
  • 5 5
  • 6 6
  • 7 7
  • 8 8
  • 9 9
  • 10 10
  • 11 11
  • 12 12
  • 13 13
  • 14 14
  • 15 15
  • 16 16
  • 17 17
  • 18 18
  • 19 19
  • 20 20
  • 21 21
  • 22 22
  • 23 23
  • 24 24
Thank for your help!
  2 Comments
the cyclist
the cyclist on 27 Aug 2015
Edited: the cyclist on 27 Aug 2015
I don't understand the rule you are trying to explain, or your example. Can you use actual MATLAB commands to define your input/output, such as
x = [1 10; 2 11; etc]
y = [1 2; 2 2; 3 3; etc]
and give another example or two of input/output?
Camilo  Corredor
Camilo Corredor on 27 Aug 2015
the vector length and the elements are random between 0 and 24. However the elements are sorted but they do not correspond with index vector. For example:
This vector
Left side = index vector Right side = content vector
Then,
  • 1 3
  • 2 4
  • 3 5
  • 4 10
  • 5 11
  • 6 13
  • 7 14
  • 8 15
  • 9 16
Then, I must to have a length vector equal to 24, where the content of every index vector be equal to index. But if any number does not exist, then one could put in that element vector 0 or NaN.
I thank your help. If is possible, this is my email cami23.cr23@gmail.com Maybe we can talk by hangout It's very important, this is for my undergraduate thesis

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 27 Aug 2015
Edited: Stephen23 on 27 Aug 2015
For clarity I have not included the indices, but these are simple to include if required.
>> inp = [10;11;12;15;16;17;18;19;20;22;24];
>> out = nan(24,1);
>> out(inp) = inp
out =
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
10
11
12
NaN
NaN
15
16
17
18
19
20
NaN
22
NaN
24
Note that I assumed that the minimum value is one, as this is what the example values show (and which contradict the OP's original question which states a minimum value of zero).
  6 Comments
Stephen23
Stephen23 on 28 Aug 2015
Edited: Stephen23 on 28 Aug 2015
If you have another vector of values vec with the same number of elements as inp, then you can use much the same indexing as in the answers I gave you:
>> inp = [0;10;11;12;15;16;17;18;19;20;22;24];
>> vec = [0,01,11,21,51,61,71,81,90,02,22,42];
>> out = nan(25,2);
>> out(inp+1,1) = inp;
>> out(inp+1,2) = vec
out =
0 0
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
10 1
11 11
12 21
NaN NaN
NaN NaN
15 51
16 61
17 71
18 81
19 90
20 2
NaN NaN
22 22
NaN NaN
24 42
You can learn how these basic commands work by doing MATLAB's introductory tutorials:
Camilo  Corredor
Camilo Corredor on 31 Aug 2015
Thank you!
I solved my problem! :)

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!