Mathematica Mathematica which function would average an output

AI Thread Summary
The discussion revolves around averaging a set of arrays in Mathematica, specifically focusing on a user with 128 arrays, each containing 21 elements. Initial suggestions included converting the arrays into a single large array for averaging or using built-in functions in Mathematica. Several code snippets were provided, demonstrating how to calculate the average of each array and the overall average using functions like Total and Length. The conversation also touched on understanding specific Mathematica code related to data processing, with requests for clarification on various functions and their applications. Participants shared insights into using graphics commands to overlay text on plots, including how to change text colors and display coordinates at specified locations. The discussion concluded with successful implementations of the suggested methods, highlighting the collaborative nature of problem-solving in programming contexts.
Haitham
Messages
15
Reaction score
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
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;
}
 
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.
 
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]
 
Great I will try this! Thanks the help!
 
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[]
 
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.
 
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.
 
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!
 

Similar threads

Replies
1
Views
2K
Replies
0
Views
546
Replies
1
Views
2K
Replies
6
Views
3K
Replies
12
Views
2K
Replies
8
Views
2K
Replies
2
Views
3K
Replies
6
Views
4K
Back
Top