2
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – C. E.
    Aug 9 '14 at 21:24
  • $\begingroup$ Update: Please observe how Algohi formatted your question, and next time do the same. $\endgroup$
    – C. E.
    Aug 9 '14 at 21:27
  • $\begingroup$ 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? $\endgroup$
    – seismatica
    Aug 9 '14 at 21:30
  • $\begingroup$ 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. $\endgroup$
    – seismatica
    Aug 9 '14 at 22:19
  • $\begingroup$ @ 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. $\endgroup$
    – ramesh
    Aug 9 '14 at 22:45
5
$\begingroup$

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

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

$\endgroup$
2
  • $\begingroup$ Thanks for your time. Its fancy and works. Great job!!! $\endgroup$
    – ramesh
    Aug 10 '14 at 1:42
  • $\begingroup$ thank you @rkadhikari, glad it worked for you. $\endgroup$
    – kglr
    Aug 12 '14 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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