Message Boards Message Boards

0
|
4779 Views
|
10 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Obtaining results in a List?

Posted 2 years ago

I have a very basic and simple question that I just cannot figure out myself. I spent several hours trying and looking in the help, but no dice.

I have a List (r) which contains some Integers. I have made a Do loop to get all the sums of all Integers up to Integer[[i]]

r = {0, 1 , 4, 3, 1, 3, 4 , 8 };

length = Length[r];

m = 0;

rAdded = Do[Print[m = m + r[[i]] ], {i, 1, length}]

This gives the correct sums : 0 1 5 8 9 12 16 24

The thing is, I need them in a new List, not as a printout on my screen.

I tried with List[], Table[] and more, but nothing works.

So the result should be in the form of newList ={0,1,5,8,9,12,16,24}

But how do I do that?

POSTED BY: B. Cornas
10 Replies
Posted 2 years ago

Good tip. I have used it sometimes, but I guess Accumulate did not tip me off. I will start to use the "See Also" more :-)

POSTED BY: B. Cornas

One of the most important parts of the documentation in Mathematica is the "See Also" (either the drop down menu at the top of the help window or the list at the bottom). If you go to a function that is close, you will likely find what you need quickly. For example if you ask for help for "Total" or "Plus", Accumulate is in Both of the "See Also" dropdowns. Wolfram Research did a good job in organizing this part of the documentation

Regards,

Neil

POSTED BY: Neil Singer
Posted 2 years ago

Good idea. Thanks.

POSTED BY: B. Cornas

I find that the best way to get help is to use Google (or your favorite search engine). It’s often more useful than poring through the documentation. Chances are, you’re not the first person trying to figure out whatever it is you’re trying to do. For example, searching on “Mathematica partial sums of a series” goes right to Accumulate, and a lot of other good discussion.

POSTED BY: John Shonder
Posted 2 years ago

Great. I think it's a good sign if a language offers a couple of different ways to solve a problem. I'll look into FoldList as well. I am just taking my first steps with WL.

POSTED BY: B. Cornas
Posted 2 years ago

Thanks a million, Rohit, this really helps. I'll study the guide page you mention.

I had seen Spow and Reap a while ago mentioned in a book. I thought it was quite advanced, so I skipped it over. I will study that too Lots to learn still :-)

POSTED BY: B. Cornas
Posted 2 years ago

The functional programming way to do it

FoldList[Plus, r]
(* {0, 1, 5, 8, 9, 12, 16, 24} *)
POSTED BY: Rohit Namjoshi
Posted 2 years ago

This guide page is worth studying.

Using AppendTo

m = 0;
result = {};
Do[AppendTo[result, m = m + r[[i]]], {i, 1, Length@r}];
result
(* {0, 1, 5, 8, 9, 12, 16, 24} *)

Using Sow and Reap, which is more efficient than AppendTo.

m = 0;
Do[Sow[m = m + r[[i]]], {i, 1, Length@r}] // Reap // Last // First
(* {0, 1, 5, 8, 9, 12, 16, 24} *)
POSTED BY: Rohit Namjoshi
Posted 2 years ago

This works well and is much more elegant than my Do loop. Thanks Rohit. The thing that worries me a bit - beside my stupidness :-) , is that I didn't manage to find this solution myself. And I spent some hours trying, reading and searching. Also the ?List searche did not help me, as well as the natural langiage input with the question.

Still I am curious, how would I get a List with my inelegant Do Loop? I ask this, because a lot of examples online in the help give the final result by Print. Just to learn I'd like to know. But of course I'll use Accumulate in my script now :-)

POSTED BY: B. Cornas
Posted 2 years ago

Here is one way

r = {0, 1, 4, 3, 1, 3, 4, 8};
Accumulate[r]
(* {0, 1, 5, 8, 9, 12, 16, 24} *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract