0
$\begingroup$

everyone! I'm new in Mathematica and I'm struggling quite a lot. Right now, I'm trying to perform some calculations out of a bunch of lists I already have listed from 1 to 131 and that have a format as follows:

list1={"String", number, {x1,y1}, {x2,y2} {number,..., number}}
list2={"String", number, {x1,y1}, {x2,y2}, {number,..., number}}

And so on... Basically, I want to do some calculations with specific elements of my list, create a new list each time and export it.

By now, I have this function:

F[list1_] := Do[esc = list[[2]];
dist = Norm[list[[4]] - list[[3]]];
real = {list[[1]]};
AppendTo[real, list[[5]]*esc/dist]; Return[real]]

But like this I have to type F[tc1] and so on until F[131] and keep exporting each time. If you have any ideas, I would be really grateful.

$\endgroup$
4
  • 3
    $\begingroup$ Make a list of all your lists, then Map your function over this list. $\endgroup$
    – MarcoB
    Nov 21, 2016 at 5:32
  • $\begingroup$ what you plan to do is unclear you have two lists list1 and list2 but only list appears in your code. We must clearly understand your problem to help you. $\endgroup$ Nov 21, 2016 at 7:03
  • $\begingroup$ It would be helpful if you show an example of two simplified lists (that is, as short as possible, but having all features of your original ones). Then also an operation that you need to make and show, how the resulting list should look like. Then it will be easy to understand and to help you. $\endgroup$ Nov 21, 2016 at 8:19
  • $\begingroup$ According to your data template, list[[2]] will be a string. Yet in your code you do arithmetic with it. That seems wrong to me. Can you clarify? $\endgroup$
    – m_goldberg
    Nov 22, 2016 at 12:36

1 Answer 1

1
$\begingroup$

Assuming that your data has the form

 {String, number, number, number, {number,..., number}}

rather than what you have actually posted, then perhaps the following will help you.

f[list_List] :=
  Module[{dist},
    dist = Norm[list[[4]] - list[[3]]];
    {list[[1]], list[[5]]*list[[2]]/dist}]

Now let's cook up some data.

data = {#, 42., 3., 5., Range[2.5, 6., 1.1]} & /@ CharacterRange["A", "D"]

{{"A", 42., 3., 5., {2.5, 3.6, 4.7, 5.8}}, {"B", 42., 3., 5., {2.5, 3.6, 4.7, 5.8}}, {"C", 42., 3., 5., {2.5, 3.6, 4.7, 5.8}}, {"D", 42., 3., 5., {2.5, 3.6, 4.7, 5.8}}}

Now we can process all the data by mapping f over the data list.

f /@ data

{{"A", {52.5, 75.6, 98.7, 121.8}}, {"B", {52.5, 75.6, 98.7, 121.8}}, {"C", {52.5, 75.6, 98.7, 121.8}}, {"D", {52.5, 75.6, 98.7, 121.8}}}

Notes

  1. Don't use Do to make a compound expression. It is an iterating construct not a grouping construct. Believe it or not, semicolon ( ; ) is the grouping operator in Mathematica, not a delimiter as it in many other programming languages. Reference: CompoundExpression

  2. Module is the Mathematica construct used to group code when you want break the code up into steps by assigning partial results to local variables. Hence, Module[{dist}, ...].

  3. You almost never need to use Return because a function normally returns the last expression that it evaluates. Hence, {list[[1]], list[[5]]*list[[2]]/dist} is return by f.

Here are some links to documentation pages that will help you understand what I done.

http://reference.wolfram.com/language/howto/MapAFunctionOverAList.html

http://reference.wolfram.com/language/tutorial/FunctionsAsProcedures.html

What are the most common pitfalls awaiting new users?

$\endgroup$
0

Your Answer

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

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