how to arrange in order index by number of position

Dear,
I have this
row =[4 5 2 2 2 1];
its index as Known is [1 2 3 4 5 6]; I want to arrange this index according to the number of the row from right to left, elements of row is the unfilled position in the final row I want to put the index in it, so I wrote this but I stuck with the filling. I will explain it more. Starting from right to left, for
idx=[1 2 3 4 5 6];
idx=1 take position 1 as in row=1 , idx=2 take position 2, then idx=3 take position 2 again instead number idx 2,then idx=4 take position 2 again instead idx 3, then idx=5 take position 5, then idx=6 take position 4, I should get [5 2 6 3 4 1]. I need to arrange the index for any row in this method. What I wrote is very wrong.
I appreciate any help.
row =[4 5 2 2 2 1];
fliprow=flip(row)
% Mapping Row
idx=find(row);
S=[];
for s=1:g
d= fliprow(:,s)
S=[S d]
end
S
Nadia,

4 Kommentare

That is one very unclear explanation! Your comment to arich82 clarify a few things but I've still not got any idea how you get your final result.
For a start, it would help if you use left to right indexing / ordering like everybody else and like matlab does. You can always fliplr your inputs / outputs if required. I understand that your (badly named) row is supposed to be read from right to left, but I've got no idea if your (equally badly named) idx is supposed to be read right to left, or left to right.
Using left to right indexing, your input is
permutationrule = [1 2 2 2 5 4];
topermute = [1 2 3 4 5 6] %possibly [6 5 4 3 2 1]
After that I have no idea what you do. Please show step by step instructions.
Dear Guillaume,
sorry for badly named, I think you right. I will use your names. as I said. This is my permutation rule [4 5 2 2 2 1], if we flip it to [1 2 2 2 5 4], then we still can depend on it from left to right by using their index [1 2 3 4 5 6]. I think its ok. but I still to start from right to write the final one [5 2 6 3 4 1] or start from left so I get [1 4 3 6 2 5] then flip it. if we start from right then the idea is to put the number of the index in the corresponding position from permutationrule starting from index1 then we put the index 2, then because index 3 take also position 2 we push the previous one (index2) to left to fill (index3) then we push the later to the left also to put index4 in position 2, then we put index5 in position 5 then we push index2 to the left to put index6. depending on this statement (That is Pi = k where i = f ( j ). here f is a counting function that determines i as the j-th unfilled position in the permutation counting from right-to-left. )
I attached a picture showing that. Sorry because its not very clear.
Regards,
Nadia
Your diagram clarifies things a lot. You've never mentioned shifting/sliding numbers before, so that step was not apparent.
I am so sorry,
I thought what I explained is clear. Its confusing and difficult to me to program it.
regards,

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Guillaume
Guillaume am 7 Okt. 2015
Now that we have clear explanations, it's easy to compute the result of your algorithm.
As mentioned in my comments, I'm going to use the natural left-to-right indexing. You're free to permute the input and output with fliplr.
The following may not be the most efficient implementation in term of memory (it does a fair number of reallocation). It's a straightforward implementation of your algorithm:
permutationrule = [1 2 2 2 5 4] %your input
permutated = []; %the output
for idx = 1:numel(permutationrule)
insertionpoint = permutationrule(idx);
permutated = [permutated(1:insertionpoint-1), idx, permutated(insertionpoint:end)];
end
It's simple, permutationrule(idx) tells you where idx needs to be inserted in permutated. So you jut have to concatenate, the portion of permutated before the insertion point (i.e. permutated(1:insertionpoint-1)), the insertion (i.e. idx), and the portion of permutated from the insertion point to the end (i.e. permutated(insertionpoint:end)).

2 Kommentare

Thanks Guillaume ,
that's great. I will try to use it for any size for permutationrule .
Great thanks,
Nadia
  
The code already works for any length of permutationrule.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

arich82
arich82 am 6 Okt. 2015
I wonder if, perhaps, there is an error in the expected solution you posted, and if it should instead be:
S = [5, 2, 4, 6, 3, 1].';
If so, this looks suspiciously like the permutation swaps used in Numerical Recipes for e.g. bringing the permuted L*U back into A order:
row =[4 5 2 2 2 1];
n = numel(row);
S = (1:n).';
for k = n:-1:1
swap = S(k, :);
S(k, :) = S(row(k), :);
S(row(k), :) = swap;
end
If not, you'll need to more clearly describe the intermediate result after each step, as I can't seem to get anything close to your expected result.
Please accept this answer if it helps, or clarify in the comments if I'm misinterpreting something.

1 Kommentar

Dear,
Thank you very much for helping me. That's really will help me. Its exactly as you expect, is a permutation. But a little but different. I know what I wrote in my code is totally wrong, but to write general formula for this permutation is a bit tricky.
This is an explanation for what I want to do.
The string (452221) is mapped into a g-th order permutation as follows: For the string position index k ranging from g down to 1, place k at position i of the permutation (i.e., Pi). That is Pi = k where i = f ( j ). Here f is a counting function that determines i as the j-th unfilled position in the permutation counting from right-to-left. A simple example illustrates the above. If g = 6, a stringencoding obeying the range bounds for each position is: (452221) and its corresponding permutation is:(526341). I will try work on it and what I can do.
Many thanks for both of you.
Nadia,

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 6 Okt. 2015
I've read this many times and still can't understand what you're trying to say. First of all "row" is only one row, so "number of the row from right to left" is just going to be 1 always. Unless row means the vector and not the row. Why don't you rename your vector "vec" instead of row? I don't even know why you're talking about row(s) when everything you've mentioned is just a single row - there is no column vector or 2D matrix.
If you just want to reorder some vector, vec2, according to some sorting order given in vec1 from right to left (reverse order), then just do
vec1 =[4 5 2 2 2 1];
vec2 = vec2(fliplr(vec1));

1 Kommentar

Dear,
thanks for pay attention for my question, I explain it in my comment above. Its a permutation of [1 2 3 4 5 6] depending on [4 5 2 2 2 1].
Many thanks

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by