Sorting data from specific row

1 vue (au cours des 30 derniers jours)
BeeTiaw
BeeTiaw le 30 Nov 2018
Commenté : Image Analyst le 1 Déc 2018
Hi expert,
Suppose that I have the following [n x 4] matrix where the last column is the row index.
I want to sort this matrix based on its index starts from row 58, i.e. row 58 becomes row 1.
I have copied the expected result below. As you can see, idx 58 has now become idx 1. I have created this output manually and wondering if we can create a program for this operation.
I hope it is easier for you to understand what I am trying to achieve.
data=[
% C x y idx
%=======================================
5.6073 -0.0976 -0.9941 1.0000
13.4173 -0.2308 -0.9675 2.0000
22.4240 -0.3846 -0.9320 3.0000
32.8868 -0.5414 -0.8373 4.0000
39.9133 -0.6509 -0.7781 5.0000
48.8753 -0.7692 -0.6716 6.0000
55.0737 -0.8432 -0.5888 7.0000
60.7391 -0.9083 -0.5089 8.0000
65.5056 -0.9675 -0.4408 9.0000
70.2820 -1.0237 -0.3669 10.0000
75.0905 -1.0888 -0.2899 11.0000
77.7703 -1.1331 -0.2456 12.0000
81.1032 -1.1716 -0.1834 13.0000
84.2605 -1.2367 -0.1243 14.0000
86.7016 -1.2840 -0.0740 15.0000
90.7816 -1.3047 0.0178 16.0000
93.7990 -1.3373 0.0888 17.0000
97.7415 -1.3491 0.1834 18.0000
101.9127 -1.3462 0.2840 19.0000
105.4733 -1.3254 0.3669 20.0000
109.3020 -1.2840 0.4497 21.0000
111.2655 -1.2544 0.4882 22.0000
113.0971 -1.2071 0.5148 23.0000
115.9221 -1.1686 0.5680 24.0000
119.2205 -1.1213 0.6272 25.0000
122.2223 -1.0562 0.6657 26.0000
124.7305 -1.0030 0.6953 27.0000
128.5472 -0.9320 0.7426 28.0000
133.2641 -0.8550 0.8047 29.0000
138.3187 -0.7692 0.8639 30.0000
142.1259 -0.7041 0.9053 31.0000
146.3541 -0.6183 0.9290 32.0000
150.1586 -0.5533 0.9645 33.0000
154.2771 -0.4675 0.9704 34.0000
159.8625 -0.3580 0.9763 35.0000
167.3069 -0.2219 0.9852 36.0000
174.1348 -0.1006 0.9793 37.0000
179.8245 -0.0030 0.9793 38.0000
182.0951 0.0355 0.9704 39.0000
188.6575 0.1464 0.9615 40.0000
194.0578 0.2352 0.9393 41.0000
198.1323 0.3018 0.9216 42.0000
204.6068 0.4038 0.8817 43.0000
209.8847 0.4837 0.8417 44.0000
217.9093 0.6036 0.7751 45.0000
223.9603 0.6790 0.7041 46.0000
228.7075 0.7411 0.6509 47.0000
233.1990 0.7988 0.5976 48.0000
237.3932 0.8787 0.5621 49.0000
240.8265 0.9275 0.5178 50.0000
245.2960 0.9808 0.4512 51.0000
248.9525 1.0340 0.3979 52.0000
252.7702 1.0828 0.3358 53.0000
256.3519 1.1272 0.2737 54.0000
260.5377 1.1538 0.1923 55.0000
264.5124 1.2012 0.1154 56.0000
267.3981 1.2367 0.0562 57.0000
269.8639 1.2633 0.0030 58.0000
272.1048 1.2870 -0.0473 59.0000
275.2524 1.3195 -0.1213 60.0000
276.7260 1.3550 -0.1598 61.0000
279.5405 1.3905 -0.2337 62.0000
282.2006 1.4231 -0.3077 63.0000
285.6266 1.4172 -0.3964 64.0000
287.9931 1.3846 -0.4497 65.0000
289.9505 1.3284 -0.4822 66.0000
293.5338 1.2840 -0.5592 67.0000
296.9429 1.2515 -0.6361 68.0000
300.1697 1.2367 -0.7189 69.0000
303.1574 1.1864 -0.7751 70.0000
305.9150 1.1479 -0.8314 71.0000
308.6397 1.0769 -0.8609 72.0000
311.1835 1.0178 -0.8905 73.0000
314.8198 0.9408 -0.9349 74.0000
316.9771 0.8698 -0.9320 75.0000
319.5037 0.7959 -0.9320 76.0000
321.3839 0.7160 -0.8964 77.0000
323.9515 0.6568 -0.9024 78.0000
327.0877 0.5917 -0.9142 79.0000
330.8083 0.5207 -0.9320 80.0000
338.2939 0.3757 -0.9438 81.0000
345.6466 0.2574 -1.0059 82.0000
355.9860 0.0710 -1.0118 83.0000];
The expected output is below
aftersorting=[
% C x y idx newidx
%=======================================
269.8639 1.2633 0.003 58 1
267.3981 1.2367 0.0562 57 2
264.5124 1.2012 0.1154 56 3
260.5377 1.1538 0.1923 55 4
256.3519 1.1272 0.2737 54 5
252.7702 1.0828 0.3358 53 6
248.9525 1.034 0.3979 52 7
245.296 0.9808 0.4512 51 8
240.8265 0.9275 0.5178 50 9
237.3932 0.8787 0.5621 49 10
233.199 0.7988 0.5976 48 11
228.7075 0.7411 0.6509 47 12
223.9603 0.679 0.7041 46 13
217.9093 0.6036 0.7751 45 14
209.8847 0.4837 0.8417 44 15
204.6068 0.4038 0.8817 43 16
198.1323 0.3018 0.9216 42 17
194.0578 0.2352 0.9393 41 18
188.6575 0.1464 0.9615 40 19
182.0951 0.0355 0.9704 39 20
179.8245 -0.003 0.9793 38 21
174.1348 -0.1006 0.9793 37 22
167.3069 -0.2219 0.9852 36 23
159.8625 -0.358 0.9763 35 24
154.2771 -0.4675 0.9704 34 25
150.1586 -0.5533 0.9645 33 26
146.3541 -0.6183 0.929 32 27
142.1259 -0.7041 0.9053 31 28
138.3187 -0.7692 0.8639 30 29
133.2641 -0.855 0.8047 29 30
128.5472 -0.932 0.7426 28 31
124.7305 -1.003 0.6953 27 32
122.2223 -1.0562 0.6657 26 33
119.2205 -1.1213 0.6272 25 34
115.9221 -1.1686 0.568 24 35
113.0971 -1.2071 0.5148 23 36
111.2655 -1.2544 0.4882 22 37
109.302 -1.284 0.4497 21 38
105.4733 -1.3254 0.3669 20 39
101.9127 -1.3462 0.284 19 40
97.7415 -1.3491 0.1834 18 41
93.799 -1.3373 0.0888 17 42
90.7816 -1.3047 0.0178 16 43
86.7016 -1.284 -0.074 15 44
84.2605 -1.2367 -0.1243 14 45
81.1032 -1.1716 -0.1834 13 46
77.7703 -1.1331 -0.2456 12 47
75.0905 -1.0888 -0.2899 11 48
70.282 -1.0237 -0.3669 10 49
65.5056 -0.9675 -0.4408 9 50
60.7391 -0.9083 -0.5089 8 51
55.0737 -0.8432 -0.5888 7 52
48.8753 -0.7692 -0.6716 6 53
39.9133 -0.6509 -0.7781 5 54
32.8868 -0.5414 -0.8373 4 55
22.424 -0.3846 -0.932 3 56
13.4173 -0.2308 -0.9675 2 57
5.6073 -0.0976 -0.9941 1 58
355.986 0.071 -1.0118 83 59
345.6466 0.2574 -1.0059 82 60
338.2939 0.3757 -0.9438 81 61
330.8083 0.5207 -0.932 80 62
327.0877 0.5917 -0.9142 79 63
323.9515 0.6568 -0.9024 78 64
321.3839 0.716 -0.8964 77 65
319.5037 0.7959 -0.932 76 66
316.9771 0.8698 -0.932 75 67
314.8198 0.9408 -0.9349 74 68
311.1835 1.0178 -0.8905 73 69
308.6397 1.0769 -0.8609 72 70
305.915 1.1479 -0.8314 71 71
303.1574 1.1864 -0.7751 70 72
300.1697 1.2367 -0.7189 69 73
296.9429 1.2515 -0.6361 68 74
293.5338 1.284 -0.5592 67 75
289.9505 1.3284 -0.4822 66 76
287.9931 1.3846 -0.4497 65 77
285.6266 1.4172 -0.3964 64 78
282.2006 1.4231 -0.3077 63 79
279.5405 1.3905 -0.2337 62 80
276.726 1.355 -0.1598 61 81
275.2524 1.3195 -0.1213 60 82
272.1048 1.287 -0.0473 59 83
];

Réponse acceptée

Image Analyst
Image Analyst le 30 Nov 2018
Try this:
aftersorting = circshift(flipud(data), 58)
  3 commentaires
BeeTiaw
BeeTiaw le 1 Déc 2018
can you explain what is this line actuall does?
Image Analyst
Image Analyst le 1 Déc 2018
flipud() flips the matrix up/down so that the last row is at the top, and the first row is at the bottom. So now row 81 is at the top and row 1 is at the bottom.
circshift() just circulalry shifts the array upwards by some amount. Rows that shift up past the first row, reappear on the bottom row. Kind of like you just rotated your belt around your waist.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by