Message Boards Message Boards

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

Run a code multiple times and each time it returns a value?

Posted 6 years ago

Hello, I have a code that I want to run multiple times. In Matlab, I can enclose the code within a function and call the function every time I want to run the code. In each iteration, I should pass multiple values to the variables within the code and then the code runs and return multiple values. I wonder if this can be done in Mathematica.

Thank you

POSTED BY: mohamed alzenad
8 Replies

I have tried this but it did not work

xList = {4, 3, 7};
yList = {1, 6, 8, 10};
g[x_, y_] := Module[{x, y},
  z = x + y;
  c = 2*y;]
Outer[g, xList, yList]

Please note that I tried to pack my code within Module

POSTED BY: mohamed alzenad

It makes no sense to localize x and y with Module. They get rewritten from the arguments, so g[4,1] produces Module[{4,1},...], which then can't be evaluated. It might make sense to localize z and c, except that they are unnecessary. Also, ; throws away its left hand side, so your g would return Null if the Module was evaluatable. Getting rid of the unnecessary z, c and Module, and constructing a list of the expressions to be returned, we get:

g[x_, y_] := {x + y, 2*y}
Outer[g, xList, yList]

yielding

{{{5, 2}, {10, 12}, {12, 16}, {14, 20}}, 
 {{4, 2}, {9, 12}, {11, 16}, {13, 20}}, 
 {{8, 2}, {13, 12}, {15, 16}, {17, 20}}}
POSTED BY: John Doty
f[x_, y_] := {x, y, g[x, y]}
Outer[f, {4, 3, 7}, {1, 6, 8, 10}]

yielding

{{{4, 1, g[4, 1]}, {4, 6, g[4, 6]}, {4, 8, g[4, 8]}, {4, 10,  g[4, 10]}}, 
{{3, 1, g[3, 1]}, {3, 6, g[3, 6]}, {3, 8, g[3, 8]}, {3,10, g[3, 10]}}, 
{{7, 1, g[7, 1]}, {7, 6, g[7, 6]}, {7, 8, g[7, 8]}, {7, 10, g[7, 10]}}}
POSTED BY: John Doty

Are you perhaps looking for Outer?

POSTED BY: John Doty

Thank you for your replies. I will be explain what I want to do more. I have two variables xList and yList with a list of values where xList={4,3,7} and yList={1,6,8,10}. I wrote a code and tested it for a particular value for xList and yList, e.g., x=4 and y=8 and worked well. Now, I want to modify my code such that I can run the code 3*4=12 times (xList size * yList size). The variables within my code are named x and y and they will take values from xList and yList respectively. For example, in the first iteration x=4 and y=1 and in the second iteration x=4 and y=6. In the last iteration, x=7 and y=10. The code should return 3 variables (e,g., a, b and c) each of them is a list of 12 values.

POSTED BY: mohamed alzenad
Anonymous User
Anonymous User
Posted 6 years ago
In[1]:= xList = {4, 3, 7}

Out[1]= {4, 3, 7}

In[2]:= yList = {1, 6, 8, 10}

Out[2]= {1, 6, 8, 10}

In[3]:= Table[{x, y, f[x, y]}, {x, xList}, {y, yList}]

Out[3]= {{{4, 1, f[4, 1]}, {4, 6, f[4, 6]}, {4, 8, f[4, 8]}, {4, 10, 
   f[4, 10]}}, {{3, 1, f[3, 1]}, {3, 6, f[3, 6]}, {3, 8, f[3, 8]}, {3,
    10, f[3, 10]}}, {{7, 1, f[7, 1]}, {7, 6, f[7, 6]}, {7, 8, 
   f[7, 8]}, {7, 10, f[7, 10]}}}
POSTED BY: Anonymous User
Anonymous User
Anonymous User
Posted 6 years ago

"Thread":

f[x_] := List[x]

f[{1, 2, 3}]

Out[677]= {{1, 2, 3}}

In[678]:= Thread[f[{1, 2, 3}]]

Out[678]= {{1}, {2}, {3}}
POSTED BY: Anonymous User

Perhaps this would be helpful.

POSTED BY: John Doty
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