Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!! :))

share|improve this question
    
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. –  Pickett Aug 9 at 21:24
    
Update: Please observe how Algohi formatted your question, and next time do the same. –  Pickett Aug 9 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? –  seismatica Aug 9 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. –  seismatica Aug 9 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. –  rka Aug 9 at 22:45

2 Answers 2

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

Do[
 matfinal[i] = SortBy[matinitial, Total[#[[i ;; i + 4]]] &],
 {i, 6}
 ]
share|improve this answer
    
is sorting based on sum of columns from i to i+4 or sum of rows from i to i+4. Sorry for confusion. –  rka Aug 9 at 21:36
    
@rkadhikari Column i to column i+4 because in the second argument # represents a row. –  Pickett Aug 9 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. –  seismatica Aug 9 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. –  rka Aug 9 at 22:30
    
@Pickett, Thank you so much. I think it serves my purpose. –  rka Aug 9 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}]}] &

enter image description here

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]

enter image description here

share|improve this answer
    
Thanks for your time. Its fancy and works. Great job!!! –  rka Aug 10 at 1:42
    
thank you @rkadhikari, glad it worked for you. –  kguler Aug 12 at 1:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.