Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica which function would average an output

  1. Mar 3, 2005 #1
    I am trying to find which function would average an output that is a set of arrays and compute each element in each array in the average?

    I guess another option is that I could convert the set of arrays to one large array then average that. While I am looking online here maybe someone can give some feedback. Right now I got 128 arrays with 21 elements in each array. It is in following format A1={{18.76,.....},{17.69,.....}, .................}}

    Thanks!
     
  2. jcsd
  3. Mar 3, 2005 #2
    So you want to average the contents of three arrays?

    heres a basic idea:

    int firstarray[];
    int secondarray[];
    int thirdarray[];

    int Average(int somearray[])
    {
    int average;
    for(int i=0;i<sizeof(somearray);i++)
    {
    average += somearray;
    }
    average = average/sizeof(somearray)
    return average
    }

    int main(){
    int average = Average(firstarray[]);
    average += Average(secondarray[]);
    average += Average(thirdarray[]);
    average /= 3;

    return average;
    }
     
  4. Mar 4, 2005 #3
    Thanks Alex, That is wonderful!!

    Actually I have 128 arrays with 21 element in each. I was thinking along the lines of using one simple preexisting function or two in Mathematica.
     
  5. Mar 4, 2005 #4

    saltydog

    User Avatar
    Science Advisor
    Homework Helper

    Not sure how you are in programming Mathematica, but I wrote CalculateAvg below in Mathematica code so using the code:

    avlist=CalculateAvg[myarray]

    is a single-line command in Mathematica to do the job. aval below is just a test array to check it.

    aval = Table[Table[Random[Real, {0, 99}, 2], {6}], {10}]
    Length[aval]

    (* this routine calculate the average of each row in 2 - D array 'table' *)

    CalculateAvg[table_] := Module[{i, avg},
    avg = Table[{0}, {Length[table]}];
    For[i = 1, i <= Length[table], i++,
    avg[] = Apply[Plus, aval[]]/Length[table[]];
    ];
    Return [avg];
    ];

    avlist = CalculateAvg[aval]
     
  6. Mar 6, 2005 #5
    Great I will try this!! Thanks the help!!!!
     
  7. Mar 10, 2005 #6
    I am trying to understand this part of a mathematica code, can anyone help explain what that part does?

    Thanks!

     
  8. Mar 11, 2005 #7

    saltydog

    User Avatar
    Science Advisor
    Homework Helper

    I've looked at it. It contains function calls from an "add-on" package such as "loadFile" and other functions it's calling that are not standard Mathematica commands. You'll need to find the library that contains these functions to investigate it further. Usually these are in the AddOns/ExtraPackage directory in the Mathematica directory.
     
  9. Mar 12, 2005 #8
    Ok. To find the average of the entire set of arrays you could simply.
    Code (Text):

    b=Plus@@Flatten[A1];
    avg=b/Length[Flatten[A1]]
     
    The @@ comand is the apply funcition.

    Or if you have Mathematica 5.0 or higher you can
    Code (Text):

    Total[Flatten[A1]]/Length[A1]
     
    For finding the average for each array in the set you could
    Code (Text):

    (Plus@@@A1)/(Length/@A1)
     
    /@ comand is the map function
    The code above will return an array were each entry is the average of the array in the same position.

    Or if you want to make a function and apply it to each array you could.
    Code (Text):

    cell 1
    AvgFunction[data_]:= Total[data]/Length[data]

    cell 2
    AvgFunction/@A1
     
    Hard to tell what the code does with out seeing the functions
    GetParameterValueNextLine
    loadData
    cropData
    ffit //Fast Fourir <something> transform?

    Also the structure of the "data" variable is unclear. 2d array 3d array, someother data sturcture.

    Well the first question was posted awhile ago so you probably have already figured out a method that works. Hopefully the above give you some more options on top of that.
     
  10. Mar 14, 2005 #9
    You all are great! I have learned something new from each and every post here. Thanks!
     
  11. Mar 16, 2005 #10
    What I understand is that this command is defined so that it turn the point of a polygon into the lines within the polygon:

    polyLines[p_] := Table[{p[[n]], p[[Mod[n, Length[p]] + 1]]}, {n,1, Length[p]}]

    But what values do I need to pass to this command and be able to excute it?
     
  12. Mar 16, 2005 #11

    plover

    User Avatar
    Homework Helper

    You need to pass in a list of vertices, e.g. {{0,0},{0,1},{1,1},{1,0}} would be a unit square. For regular polygons, you can use the Geometry`Polytopes` add-on package to get vertices. For example, with that package loaded, Vertices[Octagon] does pretty much what it looks like: make the list of the vertices of a regular octagon (as if inscribed in a circle of radius one centered at the origin).

    polyLines takes a list of vertices and produces a list of pairs of endpoints for the line segments in the polygon. However, if what you want is actual Mathematica graphics primitives rather just a list of endpoints, you can either call Line/@polyLines[vertices] or change the funtion to:
    Code (Text):
    polyLines[p_] := Table[Line[{p[[n]], p[[Mod[n, Length[p]] + 1]]}], {n,1, Length[p]}]
    Also, if you don't need to work with the segments separately, the Line graphics directive accepts a list of more than 2 points for creating a continuing series of straight lines, so the following would provide a primitive for drawing a polygon:
    Code (Text):
    Line[Vertices[Octagon][[Append[Range[NumberOfVertices[Octagon]],1]]]]
    Note if you just use Line[Vertices[Octagon]] the last point does not get connected to the first.
     
    Last edited: Mar 16, 2005
  13. Mar 23, 2005 #12
    Great! Thanks!

    In my mathematica program I am plotting some images but need to be able to point on the image and somehow display the cartesian coordinates at specific locations. I am using the ListDensityPlot command to plot my image. Any idea how I can go about this one?
     
  14. Mar 25, 2005 #13
    I do not have mathematica in front of me so this code may need to be tweaked.
    Look up the syntax for Show, Graphics and Text.
    Code (Text):

    temp=ListDensityPlot[(*what you need*)];
    Show[temp,Graphics[{Text[{posx,posy},"{posx,posy}"]}]]
     
    It goes something like this. You can overlay any text or Graphics commands over a plot or other graphics object with Show.
     
  15. Mar 28, 2005 #14
    That worked pretty good! It overlays specified text at a certain coordinates entered!!!

    temp = ListDensityPlot[E^Chop[dwi[[2]][[4]]],PlotRange -> All, Mesh -> False];

    Show[temp, Graphics[{Text[{p}, {80, 90}]}]]

    I now just need to change the text to a different color so I may easily view it!!

    Can this code be modified to accept a list of coordinates instead of just one then display a line connecting these points forming a polygon?

    That would help me vertify that a list of x and y points do actually contain the region of desire.

    Thank you very much!!!! That has been a great help!
     
    Last edited: Mar 28, 2005
  16. Mar 28, 2005 #15
    Glad to know that it help.
    If you have a list of points in a array or set {{1,2},{3,4},{5,6},.....}
    And another array with the text you want to use {"text1","text2"."text3",......}
    Then use the command Table inside the Graphics command.
    example of the table
    Code (Text):

    [B]input[/B]
    Table[n,{n,1,10,1}] (*n goes from 1 to 10 in steps of 1.  If the step size is left of it will default to a size of 1*)
    [B]output[/B]
    {1,2,3,4,5,6,7,8,9,10}
    [B]input[/B]
    intarray={1,2,3};
    labelarray={"L1","L2","L3"};
    Table[{intarray[[n]],labelarray[[n]]},{n,1,3,1}]
    [B]output[/B]
    {{1,"L1"},{2,"L2"},{3,"L3"}}
     
    Also show above is how to index array with [[(*position*)]].

    You can apply the same idea to the text command.
    Code (Text):

    [B]input[/B]
    corr={{1,2},{3,4}...};
    label={"L1","L2",...};
    Table[Text[label[[n]],corr[[n]]],{n,1,Length[label]}]
    [B]output[/B]
    {Text["L1",{1,2}],Text["L2",{3,4}],...}
     
    You can then stick the output of the table into the Graphics command.

    I think you can use RGBColor[r, g, b] to change the color of the text if not then you can use TextStyle and use the option FontColor.

    Links:
    to Text:
    http://documents.wolfram.com/mathematica/functions/Text
    to Table
    http://documents.wolfram.com/mathematica/functions/Table
    Textstyle:
    http://documents.wolfram.com/mathematica/functions/TextStyle
    Graphics:
    http://documents.wolfram.com/mathematica/functions/Graphics
     
  17. Mar 29, 2005 #16
    Thanks!! This is working very well.

    Getting the default black text to change to white is not working at the moment. I tried the following and it works well independently:

    Show[Graphics[{Black, Text["Point", {0, 025}, {0, 0}, TextStyle -> {FontFamily -> "Times", FontSize ->20, FontWeight -> "Bold"}]}, PlotRange -> All]]

    But it fails when I try to use it with the earlier command:

    Show[temp, Graphics[{Table[White, Text[label[[n]], corr[[n]]], {n, 1, Length[label]},TextStyle -> {FontFamily -> "Times", FontSize ->20, FontWeight -> "Bold"}]}]]

    Not sure what might be wrong with that syntax?
     
    Last edited: Mar 30, 2005
  18. Mar 30, 2005 #17
    I think it is solved now, this is what I am using at the moment:

    corr = {{83, 225}, {91, 200}, {95, 175}, {94, 150}, {86, 115}, {80, 121}, {85, 150}, {85, 175}, {83, 200}};

    label = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    Needs["Graphics`Colors`"];

    n = Length[label];

    Show[temp, Graphics[{White, Table[Text[label[], corr[]], {i, 1, n, 1}]}]]


    Just for future reference, Do you know why the previous command was giving syntax errors or how would I have corrected it?
     
  19. Mar 30, 2005 #18
    This command?
    At least in what you posted you have the "white" command inside the table rather then the Graphics command. Or was that a mistake in the post. It looks like you solved your own problem.
     
  20. Apr 19, 2005 #19
    Yes its all resolved and working fine now. Thanks so much for your help, this was great!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Mathematica which function would average an output
Loading...