# Sort matrix rows iteratively according to sum of some columns

I have a matrix say

matinitial =
{{41, 42, 43, 44, 45, 46, 47, 48, 49, 410},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 10},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 210},
{31, 32, 33, 34, 35, 36, 37, 38, 39, 310}};


I want to form matrices matfinal[i], where i runs from 1 to 6 in this example.My matfinal[i] should be equal to matinitial after sorting rows based on sum of columns from i to i + 4. I tried the following codes :

Do[matfinal[i] = SortBy[matinitial,Total[matinitial[[All, i ;; i + 4]], {2}]], {i, 1, 6}];


But I do not get the results I desire. For example, my matfinal[1] should be equal to matinitial sorted based on Total of first four columns:

{41 + 42 + 43 + 44, 11 + 12 + 13 + 14, 21 + 22 + 23 + 24, 31 + 32 + 33 + 34} = {170, 50, 90, 130}.


Therefore,

matfinal[1]={{11, 12, 13, 14, 15, 16, 17, 18, 19, 10},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 210},
{31, 32, 33, 34, 35, 36, 37, 38, 39, 310},
{41, 42, 43, 44, 45, 46, 47, 48, 49, 410}};


Please note, in my real world problem my matinitial matrix is 100 by 20000 in size.Any help is greatly appreciated.And also, thank you for your help in advance!! :))

• Please format your code. I did it before you deleted this exact question and then reposted it. I'm not going to format the same code twice. Aug 9, 2014 at 21:24
• Update: Please observe how Algohi formatted your question, and next time do the same. Aug 9, 2014 at 21:27
• What does "sorting rows based on sum of columns from i to i + 4" mean? Could you give us an example of how such sorting is done on a matrix and what the expected result looks like? Aug 9, 2014 at 21:30
• In your latest edit, did you mean first four column or five? If i = 1, i + 4 = 5 so I think it should be the first five column. Aug 9, 2014 at 22:19
• @ seismatica, I think my issue is solved in either way. I just want to sort matrix based on sum of some columns. It could be sum of columns 1 to 4 or 1 to 5 or 4 to 7. Aug 9, 2014 at 22:45

You were almost there but you need to read up on SortBy:

Do[
matfinal[i] = SortBy[matinitial, Total[#[[i ;; i + 4]]] &],
{i, 6}
]

• is sorting based on sum of columns from i to i+4 or sum of rows from i to i+4. Sorry for confusion. Aug 9, 2014 at 21:36
• @rkadhikari Column i to column i+4 because in the second argument # represents a row. Aug 9, 2014 at 21:37
• Sorry I just evaluated matfinal from your answer and since I wasn't seeing anything I thought there was an error on yours and started to work on my answer. Now I see what you're trying to get at. I also did some benchmarking and Do seems to be faster as well compared to other methods that I tried, probably because it generates one list at a time. I will delete my answer. Aug 9, 2014 at 22:12
• @Pickett, thank you so much. I am working on my data. I will keep you posted as soon as I figure out on my data. Aug 9, 2014 at 22:30
• @Pickett, Thank you so much. I think it serves my purpose. Aug 9, 2014 at 22:47
func = Function[{mat, matfin, n, m},
Table[
With[{ord = Ordering[mat, All, Total[#[[i ;; i + m]]] < Total[#2[[i ;; i + m]]] &]},
matfin[i] = matinit[[ord]]], {i, n}]];


OP's example:

func[matinitial, matfinal, 6, 4];
matfinal[1]
(*  {{11, 12, 13, 14, 15, 16, 17, 18, 19, 10},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 210},
{31, 32, 33, 34, 35, 36, 37, 38, 39, 310},
{41, 42, 43, 44, 45, 46, 47, 48, 49, 410}} *)


Example 2:

initmat = RandomInteger[100, {4, 6}];
initmat // TableForm[#, TableHeadings -> {Array["r" <> ToString[#] &, {4}],
Array["c" <> ToString[#] &, {6}]}] &


func[initmat, finmat, 4, 2];
finmat[3]
(* {{19, 59, 6, 1, 36, 59},
{99, 75, 25, 49, 12, 10},
{79, 59, 23, 21, 88, 2},
{42, 10, 13, 97, 83, 84}} *)

Column[(TableForm[MapAt[Style[#, Red, Bold] &, finmat[#], {{All, # ;; # + 2}}],
TableHeadings -> {Array["r" <> ToString[#] &, {4}],
Array["c" <> ToString[#] &, {6}]}]) & /@ Range[4],
Dividers -> All]


• Thanks for your time. Its fancy and works. Great job!!! Aug 10, 2014 at 1:42