3
$\begingroup$

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?

Thank you in advance!

Best regards Alex

$\endgroup$
1
  • $\begingroup$ Total[Take[data,range]] $\endgroup$
    – rhermans
    May 15, 2017 at 9:51

1 Answer 1

2
$\begingroup$

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]]
$\endgroup$
6
  • $\begingroup$ 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? :) $\endgroup$ May 12, 2017 at 13:04
  • 2
    $\begingroup$ sumData=Total[data[[#]] & /@ Range[2000]] (or replace 2000 with Length[data]). $\endgroup$
    – bill s
    May 12, 2017 at 13:07
  • $\begingroup$ 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! $\endgroup$ May 12, 2017 at 13:13
  • $\begingroup$ @AlexanderHempfing No problem, edited the answer. $\endgroup$
    – Stitch
    May 12, 2017 at 13:30
  • $\begingroup$ 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. $\endgroup$
    – bill s
    May 12, 2017 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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