Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.6K Views
|
10 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Apply a function to multiple lists?

Posted 9 years ago
POSTED BY: Brandon Davis
10 Replies
Posted 9 years ago

Or...

testfunc[x_, y_, z_, t_] := x + y + z + t;

testfunc[#1, #2, #3, #4] & @@@ {{1, 2, 3, 4}, {5, 6, 7, 8}}

To me, it is not always intuitive as to how to use the "@" operator against lists, but interpreting the resultant error messages and trial and error often lead to the appropriate solution.

POSTED BY: Joel Gilly

Use

Map[costperfish[#]&,{{50,4,2.5,2},{20,6,1,2.5}}]
POSTED BY: l van Veen
Posted 9 years ago
POSTED BY: Brandon Davis

Hi Brandon, you're using the map function with just 1 argument. that why it doesn't evaluate further. I would suggest you to invest some time on learning the Map and Apply function because they are very frequently used! What helped me was the introduction of Mr Shifrin http://www.mathprogramming-intro.org/ and the video of Mr Gaylord are also very nice http://www.wolfram.com/broadcast/s?sx=gaylord

POSTED BY: l van Veen

You may use Apply with the replaces heads at level 1 shortcut.

costperfish @@@ {{50, 4, 2.5, 2}, {20, 6, 1, 2.5}}

Hope this helps.

POSTED BY: Edmund Robinson
Posted 9 years ago
POSTED BY: Brandon Davis
Posted 9 years ago

Okay, I see now!

I appreciate your clarification.I haven't used the Map command before but now I see what its doing. Thanks for clearing that up.

Brandon

POSTED BY: Brandon Davis

My bad :) You can do it in a lot of ways..

costperfish[{pondcost_, nsides_, length_, depth_}] := N[ pondcost/numfish[nsides, length, depth]];
costperfishOriginal[pondcost_, nsides_, length_, depth_] :=  N[pondcost/numfish[nsides, length, depth]];

The costperfish has now a list as argument and that is exact what you want. Now it's easy to map it!

data = {{50, 4, 2.5, 2}, {20, 6, 1, 2.5}, {30, 5, 1, 2.5}, {20, 7, 1,   2.5}}

Map[costperfish[#] &, data]

or

Map[Apply[costperfishOriginal, #] &, data]

both give {1., 0.8, 1.76471, 0.555556} as a result

POSTED BY: l van Veen
Posted 9 years ago
POSTED BY: Brandon Davis
Posted 9 years ago
POSTED BY: Brandon Davis
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard