Mathematica which function would average an output

In summary: Try this:int firstarray[];int secondarray[];int thirdarray[];int Average[];avg = firstarray[];avg = secondarray[];avg = thirdarray[];average = avg/3;or if you want to make a function and apply it to each array you could.AvgFunction[data_]:= Total[data]/Length[data];
  • #1
Haitham
15
0
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!
 
Physics news on Phys.org
  • #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;
}
 
  • #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.
 
  • #4
Haitham said:
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.

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
}];
For[i = 1, i <= Length
, i++,
avg[ ] = Apply[Plus, aval[]]/Length[table[]];
];
Return [avg];
];

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

Thanks!

procimndDWI[file_, row_, col_] :=
Module[{imndfile, data, bvals, d2, dimX, dimY, dimZ, nslices, dwidata, ffit,
hmm},

imndfile = file <> "/imnd";
(*get the data *)
data = loadData[file];
bvals = GetParameterValueNextLine[imndfile, "##$IMND_diff_b_value*"];

(*now crop the data*)
d2 = cropData[data, row, col];
dimX = Length[d2[[1]]];
dimY = Length[d2[[1]][[1]]];
dimZ = Length[bvals];
nslices = Length[d2]/dimZ;
dwidata = Transpose[Partition[Log[Flatten[d2]], dimX*dimY*nslices]];
ffit = {c, d} /. FindFit[{bvals, #} // Transpose, c + d x, {c, d}, x] &;
(*fit the data*)
hmm = ffit /@ dwidata;

(*return the fitted data*)
{Partition[
Partition[Transpose[hmm][[2]], dimY], dimX],
Partition[Partition[Transpose[hmm][[1]], dimY], dimX]}

];


End[]
(*Protect[showSlice]*)
EndPackage[]
 
  • #7
Haitham said:
I am trying to understand this part of a mathematica code, can anyone help explain what that part does?

Thanks!

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.
 
  • #8
Haitham said:
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!

Ok. To find the average of the entire set of arrays you could simply.
Code:
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:
Total[Flatten[A1]]/Length[A1]

For finding the average for each array in the set you could
Code:
(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:
cell 1
AvgFunction[data_]:= Total[data]/Length[data]

cell 2
AvgFunction/@A1

I am trying to understand this part of a mathematica code, can anyone help explain what that part does?

Hard to tell what the code does without 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.
 
  • #9
You all are great! I have learned something new from each and every post here. Thanks!
 
  • #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?
 
  • #11
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:
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:
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:
  • #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?
 
  • #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:
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.
 
  • #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:
  • #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:
[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:
[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
 
Last edited by a moderator:
  • #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:
  • #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?
 
  • #18
This command?
Show[temp, Graphics[{Table[White, Text[label[[n]], corr[[n]]], {n, 1, Length[label]},TextStyle -> {FontFamily -> "Times", FontSize ->20, FontWeight -> "Bold"}]}]]

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.
 
  • #19
Yes its all resolved and working fine now. Thanks so much for your help, this was great!
 

FAQ: Mathematica which function would average an output

1. What is the function for calculating the average in Mathematica?

The function for calculating the average in Mathematica is Mean[data], where "data" represents the list of numbers or values for which the average is to be calculated.

2. Can I use the average function for non-numerical data in Mathematica?

No, the Mean function in Mathematica can only be used for numerical data. If you have non-numerical data, you can use the function NMean[data] to calculate the average.

3. How do I calculate the average of a specific column in a dataset using Mathematica?

You can use the function Mean[data[[All, column]]], where "column" represents the column number in the dataset. This will calculate the average of all the values in that particular column.

4. Can I exclude certain values from the average calculation in Mathematica?

Yes, you can use the function Mean[Select[data, criteria]] to exclude certain values from the average calculation. "criteria" represents the condition that the values must meet in order to be included in the average calculation.

5. Does Mathematica have a function for calculating the weighted average?

Yes, Mathematica has a function called WeightedData[] which can be used to calculate the weighted average of a dataset. It takes in two lists as arguments - one for the values and one for the weights.

Similar threads

Replies
1
Views
2K
Replies
1
Views
2K
Replies
6
Views
3K
Replies
12
Views
2K
Replies
8
Views
1K
Replies
2
Views
2K
Replies
2
Views
1K
Replies
6
Views
3K
Replies
1
Views
2K
Back
Top