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?

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

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?
Just sorting the data. So, in this case, that 6 should turn into a 4.
@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?
Sorry for not making myself clear.
In my case, I have numbers 1,2,3,6,10,12,15... in this column, one of them per row. This number should not be the same as the index of the row, it's a number that represents a category. So, in order to make sense, I should have 1,2,3,4,5,6...
But they should not be in order: at the position where is a 6, it should be a 4; where is a 10, it should be a 5; and so on ...
@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.
If I have:
A = [0,1,2,3,7,10,12,8,3,7,0,2,4];
I should have:
B = [0,1,2,3,7,6,9,8,3,7,0,2,4];
Got it? But I have a column like this, not a row.
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?
Sorry, I gave a bad example. This one is what really happens:
A = [0,1,2,3,6,3,10,6,6,10,12,12,0,2];
It should turn into:
B = [0,1,2,3,4,3,5,4,4,5,6,6,0,2];

Connectez-vous pour commenter.

 Réponse acceptée

A = [0,1,2,3,6,3,10,6,6,10,12,12,0,2];
[~, ~, idx] = unique(A);
t = 0:max(idx)-1;
B = t(idx);
Note: this code relies upon the expected lowest value being 0.

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

Community Treasure Hunt

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

Start Hunting!

Translated by