Message Boards Message Boards

0
|
4887 Views
|
8 Replies
|
7 Total Likes
View groups...
Share
Share this post:

How to write a specific looping math function over a list?

Posted 9 years ago

I am new to Mathematica and have been having a lot of trouble performing a specific calculation. One which is simple enough to do in Excel, but I want to do it in this program.

In Excel, you would just put, into cell B2 "=A1-B1" where B1 is a constant. So if I have a list of numbers in Column A, I can just autofill the formula in B2 down the column and get a new list.

Thus far I have discovered: FoldList[Subtract, Z, M]

where Z is my constant and M is my list of numbers. While I do not get an error at least, I think the output is reversed from what I want. In terms of the Excel formula, I believe it is subtracting B1 from A1. How do I do the calculation I am aiming to achieve?

POSTED BY: Corina Cooling
8 Replies

In Excel, you would just put, into cell B2 "=A1-B1" where B1 is a constant. So if I have a list of numbers in Column A, I can just autofill the formula in B2 down the column and get a new list.

May be I am not understanding the question fully, since I do not use Excel. But why not write

b1 = 99;
a1 = {1, 2, 3, 4};
b2 = a1 - b1
  (* {-98, -97, -96, -95} *)
POSTED BY: Nasser M. Abbasi
Posted 9 years ago

Corina,

There are some operations in Mathematica that are automatically "Listable". Subtract is one of these. So, if you want to subtract a single constant value from each of the values in a List, you can just do it directly:

{5, 10, 15} - 1

This would give you:

{4, 9, 14}

More generally, the concept of "apply this function to each element in a list" is implemented with Map:

Map[f, {5, 10, 15}]

which gives you

{f[5], f[10], f[15]}

Using an actual function instead of f:

Map[# - 1 &, {5, 10, 15}]

would give you

{4, 9, 14}

again.

POSTED BY: Eric Rimbey
Posted 9 years ago

It is perhaps also worth mentioning that the Excel case where A an B are two columns, and you wish a 3rd column C containing the differences on each row, the Mathematica equivalent is to just subtract the two lists, which is done component-wise:

In[1]:= {a, b, c} - {d, e, f}

Out[1]= {a - d, b - e, c - f}
POSTED BY: David Keith
Posted 9 years ago

I really appreciate all of the prompt responses! Unfortunately I don't feel as though I explained myself well. Like you guys seemed to understand, I definitely want to output a list. However, the list is going to be dependent on itself. So the 3rd value is going to be a function of the 2nd value. The 4th value is dependent on the 3rd value, etc. Forgive me, I lack proper mathematical terminology to best describe the situation, bear with me. The list is going to be building on itself and also another list that is already established. And it's just simply subtraction that I want to do. Like I was saying,

In Excel, you would just put, into cell B2 "=A1-B1" where B1 is a constant. So if I have a list of numbers in Column A, I can just autofill the formula in B2 down the column and get a new list

Does this better clarify? Thank you so much for any and all help!!

POSTED BY: Corina Cooling

Does this better clarify?

I think it will be much more clear if you simply give a small actual example of an input and output.

For example, if A1= {1,2,3,4} and B=99, what do you want B2 to be? Which is the result of your Excel "B=A1-B1" operation? I wrote A1 as a row here, simply to save vertical space. But pretend it is a column vector instead. I think a simple example would be much better than long explanation using just words in this case.

POSTED BY: Nasser M. Abbasi
Posted 9 years ago

Thank you so much for the words of wisdom.

N={n1,n2,n3,n4,n5,n6}

B=20

C={20-n1,c1-n2,c2-n3,c3-n4,c4-n5,c5-n6}
POSTED BY: Corina Cooling

Yes, now it is much more clear. Since C={20-n1,c1-n2,c2-n3,c3-n4,c4-n5,c5-n6} is the same as

        C={20-n1, 20-n1- n2, 20-n1-n2- n3, 20-n1-n2-n3- n4, 20-n1-n2-n3-n4-n5,  20-n1-n2-n3-n4-n5-n6}

Then one way to do it is

A0 = {n1, n2, n3, n4, n5, n6};
B0 = 20;
C0 = B0 - Accumulate[A0]  (* the excel equation *)
(* {20 - n1, 20 -n1-n2, 20 -n1-n2-n3, 20-n1-n2-n3- n4, 20-n1-n2-n3-n4-n5, 20-n1-n2-n3-n4-n5-n6} *)

Using numbers

A0 = {1, 2, 3, 4};
B0 = 20;
C0 = B0 - Accumulate[A0] (*the excel equation*)
(*  {19, 17, 14, 10} *)
POSTED BY: Nasser M. Abbasi
Posted 9 years ago

Thank you so much!!! I truly appreciate the help!

POSTED BY: Corina Cooling
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