4
$\begingroup$

I just got Mathematica from my school and I'm trying to learn the basics.

How could I use the value of a cell in a table (as in Insert->Table) and pass it as an argument to a function whose value is displayed in another cell in the same table?

For example, in my table displaying some chemistry data, my input and output cells would conceptually be something like this:

Gold   |  ElementData["CELL TO THE LEFT", "Specific Heat"]

Silver |  ElementData["CELL TO THE LEFT", "Specific Heat"]

Copper |  ElementData["CELL TO THE LEFT", "Specific Heat"]

This is kind of how it would work in a spreadsheet program.

What would be the best way to do this?

$\endgroup$

2 Answers 2

6
$\begingroup$

I hope you will realize very soon that Mathematica is not Excel. If you begin learning the basics by mimicking how you would do it in Excel, you are off to a very bad start.

Let us try to do it more in Mathematica style. First, we make a list of your elements

elms = {"Gold", "Silver", "Copper"}

Now, we make a function which takes an arbitrary element name and returns the specific heat. Then we apply (we map with /@) this function to every entry of your elements list

heats = Function[elm, ElementData[elm, "SpecificHeat"]] /@ elms

Now, we have to lists, one with the elements and one with the heats. Now, you can display them in a table if you like:

TableForm[{elms, heats}]

Mathematica graphics

Edit:

Regarding your comment: I know what you are referring to, but a direct equivalence to when Excel refers to a column does not exist. However, there are million more ways once you got used to Mathematica. For instance, let's put everything we know until now into a function which takes an arbitrary list of elements and creates the table view you want. You'll see that it's only a combination of what I showed you above

create[elms_List] := 
 TableForm[
  Transpose[{elms, 
    Function[elm, ElementData[elm, "SpecificHeat"]] /@ elms}]];

Now we use this create function to make a dynamic list of as many elements you like

With[{allElms = ElementData[]},
 Manipulate[create[allElms[[1 ;; n]]], {n, 1, Length[allElms], 1}]
 ]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Is there a way to make this dynamic? What if I didn't know what elements I was going to use before hand, but wanted to add them dynamically? Anyway, thanks for the help. $\endgroup$
    – deox
    Oct 10, 2014 at 1:58
  • 1
    $\begingroup$ @DeoxNa See my updated answer. $\endgroup$
    – halirutan
    Oct 10, 2014 at 2:11
  • $\begingroup$ That worked! Thanks for all the help. $\endgroup$
    – deox
    Oct 10, 2014 at 23:02
2
$\begingroup$

Perhaps

Table[{j, ElementData[j, "SpecificHeat"]}, {j, {"Gold", "Silver", "Copper"}}] // TableForm

enter image description here

or

tableF = {#, ElementData[#, "SpecificHeat"]} &;
tableF /@ {"Gold", "Silver", "Copper"} // TableForm
(* same output *)

or

(* initialize a 3 by 2 table: *)
table = ConstantArray[0, {3, 2}]; 
(* set the first column to the three element names: *)
table[[All, 1]] = {"Gold", "Silver", "Copper"}; 
(* set the second column as a function of the first: *)
table[[All, 2]] =  ElementData[#, "SpecificHeat"] & /@ table[[All, 1]] ;
table // TableForm 
(* same output *)
$\endgroup$

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.