how to arrange in order index by number of position
Ältere Kommentare anzeigen
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
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.
nadia nadi
am 7 Okt. 2015
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.
nadia nadi
am 7 Okt. 2015
Akzeptierte Antwort
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.
1 Kommentar
nadia nadi
am 7 Okt. 2015
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
nadia nadi
am 7 Okt. 2015
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!