# Give i an increasing value from 1 to 2000 - Suming certain columns

I would like to give i a value starting by 1 and then increasing to 2000. I have to sum up columns and would like to use i instead of typing the number every time. To visualize that I mean is as follows:

sumdata=data[[1]] + data[[2]] + data[[3]] + data[[4]] + data[[5]] + data[[6]] + data[[7]] + data[[8]] + data[[9]] + data[[10]];


Now I would used i to replace 1 to 10 in a function, however, i represents 1,2,3, etc. in increasing order:

sumdata=data[[i]] + data[[i]] + data[[i]] + data[[i]] + data[[i]] + data[[]] + data[[i]] + data[[i]] + data[[i]] + data[[i]];


Still, I am very new to programming in Mathematica. Hence it would be also nice if someone could tell me how to sum up certain columns in a matrix, without adressing each and every column itself. I have a matrix of ~2400 columns/rows and would like to sum up column 1 to 56, 57 to 112 and so on. Maybe someone has a easier approach?

Best regards Alex

• Total[Take[data,range]] May 15, 2017 at 9:51

There are many solutions. Just a few, depending on how complex the actual problem is. The main principle here is to generate a list and then Applythe function you want, in this case Plus.

sumdata[data_, length_] := Plus @@ (data[[#]] & /@ Range[length])

sumdata2[data_, length_] := Plus @@ Table[data[[i]], {i, 1, length}]

sumdata3[data_, length_] := Plus @@ data[[1 ;; length]]


For the matrix, you can use the same idea. Take any columns/rows by using [[r1;;r2,c1;;c2]] and then Apply the Plus. For example, for rows 1 through 3 and columns 2 and 3:

Plus @@ Flatten@matrix[[1 ;; 3, 2 ;; 3]]

• Hey @Stitch! Thank you very much for your immediate reply! Unfortunately, I am not really familiar with the code at hand. Could you please explain it a little? :) May 12, 2017 at 13:04
• sumData=Total[data[[#]] & /@ Range[2000]] (or replace 2000 with Length[data]). May 12, 2017 at 13:07
• Hey @bills, Thank you very much for your comment! Okay, now I understand it a little bit better, thanks! :) However, how can I distinguish which columns, to sum up. For example, I would sum up the first 56 columns, that's possible by replacing 2000 by 56, that's clear so far, but how can I select a range, say column 56 to 112? Thanks! May 12, 2017 at 13:13
• @AlexanderHempfing No problem, edited the answer. May 12, 2017 at 13:30
• Range[56, 112]. But you need to learn about the syntax of commands -- when you don't know how to use something like Range, put the cursor on the word Range and press F1 to get help, where you will see sorts of different ways you can use the command. May 12, 2017 at 13:53