I have a matrix column with a number in each row, but they are not consecutive (per example, from 3 it jumps to 6). How can I turn them into a consecutive order?
Afficher commentaires plus anciens
I have a matrix column with a number in each row, but they are not consecutive (per example, from 3 it jumps to 6). How can I turn them into a consecutive order?
9 commentaires
Walter Roberson
le 9 Jan 2017
Is this a matter of sorting the data, or is it a matter of adding new rows to "fill in" the values? If it is a matter of adding new rows, then what values should be filled in for the other columns?
Eduardo Rocha
le 9 Jan 2017
@Eduardo Rocha: your explanations do not make much sense. sorting data is a totally different thing to creating a sequence of monotonic integers, which is what your question seems to talk about. Which one do you want?
Computer programs are very precise creatures: they do exactly what you tell them to do. We can help you, but you need to help us by being as clear as possible about what you need. What would really be helpful is if you gave us complete input and output example data, together with a real explanation of what you want to achieve.
Writing things like "So, in this case, that 6 should turn into a 4." does not give us any explanation of why, or what the underlying rule is, or how the output relates to the input. There are infinite functions that could map 4 to 6: which of those infinite functions do you want to use?
Eduardo Rocha
le 9 Jan 2017
Eduardo Rocha
le 9 Jan 2017
@Eduardo Rocha: not very clear. You wrote that you want to "Just sorting the data", which would mean putting them in order. But now you state "But they should not be in order". Sooo... you want the values both in order and not in order.
A little bit of clarity would help everyone.
Eduardo Rocha
le 9 Jan 2017
Walter Roberson
le 9 Jan 2017
I really do not understand the rule to be followed! Why are only the 10 and 12 changed? Why should they become 6 and 9?? How does this generalize?
Eduardo Rocha
le 9 Jan 2017
Réponse acceptée
Plus de réponses (1)
depending on what you really mean by "sorting" and "consecutive", here are 2 different solutions:
A=randi(10,12,4)
A =
8 2 2 2
9 9 2 8
9 6 3 4
1 6 5 3
4 2 1 5
3 9 10 1
9 7 10 2
5 4 5 10
10 6 5 10
2 5 4 6
3 1 10 1
2 3 4 3
% use sort if you just want to sort them
>> A(:,3)=sort(A(:,3))
A =
8 2 1 2
9 9 2 8
9 6 2 4
1 6 3 3
4 2 4 5
3 9 4 1
9 7 5 2
5 4 5 10
10 6 5 10
2 5 10 6
3 1 10 1
2 3 10 3
% but if u want to make them consecutive => (1:#rows)
>> A(:,3)=1:12
A =
8 2 1 2
9 9 2 8
9 6 3 4
1 6 4 3
4 2 5 5
3 9 6 1
9 7 7 2
5 4 8 10
10 6 9 10
2 5 10 6
3 1 11 1
2 3 12 3
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!