Shifting position of an element in a vector from 1st to last
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ayesha Maroof
am 14 Mär. 2019
Bearbeitet: Jan
am 16 Mär. 2019
i want to change the position of a number in element from 1st to last
for example if i have a sequence 1-2-3-4-5
first i want to move 1 to end of vector like 2-3-4-5-1
then i want to move 2 like 1-3-4-5-2
then 3, 1-2-4-5-3
and so on withoutchanging order of other.i can do it with for loop but is there a single command for this in Matlab??
2 Kommentare
Akzeptierte Antwort
Stephen23
am 14 Mär. 2019
Here is a general solution for any sized vector (not in a single command though):
>> N = 5;
>> V = 1:N
V =
1 2 3 4 5
>> [~,X] = sort(eye(N),2);
>> M = V(X)
M =
2 3 4 5 1
1 3 4 5 2
1 2 4 5 3
1 2 3 5 4
1 2 3 4 5
2 Kommentare
Jan
am 16 Mär. 2019
+1. When the vector is 1:N, the single command is working already:
[~,X] = sort(eye(N),2)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!