how to arrange in order index by number of position
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
nadia nadi
am 6 Okt. 2015
Kommentiert: Guillaume
am 7 Okt. 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 Kommentare
Guillaume
am 7 Okt. 2015
Your diagram clarifies things a lot. You've never mentioned shifting/sliding numbers before, so that step was not apparent.
Akzeptierte Antwort
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
Weitere Antworten (2)
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.
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));
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!