MATLAB Answers

how to arrange in order index by number of position

6 views (last 30 days)
nadia nadi
nadia nadi on 6 Oct 2015
Commented: Guillaume on 7 Oct 2015
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 Comments
nadia nadi
nadia nadi on 7 Oct 2015
I am so sorry,
I thought what I explained is clear. Its confusing and difficult to me to program it.
regards,

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 7 Oct 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 Comments
Guillaume
Guillaume on 7 Oct 2015
The code already works for any length of permutationrule.

Sign in to comment.

More Answers (2)

arich82
arich82 on 6 Oct 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 Comment
nadia nadi
nadia nadi on 7 Oct 2015
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,

Sign in to comment.


Image Analyst
Image Analyst on 6 Oct 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 Comment
nadia nadi
nadia nadi on 7 Oct 2015
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by