2
$\begingroup$

I have a list defined as MarsDisk which, in MatrixForm, has 17 rows and 3 columns, which hold information about a photon energy level, photon flux, and uncertainty in the flux. I'm interested in taking the average of the energy levels and executed the following:

Average[MarsDisk[[i, 2]]]

and I received this error message:

Part::partw: Part 18 of {{0.8725,0.5,0.5},{0.817,0.4,0.4},[continues with the rest of my data points]} does not exist. >>

I don't get why this doesn't work because I never asked for the 18th element.

Then, I tried to create a For loop instead:

ψ = Array[c, {17, 2}];

For [i = 1, i <= 17, i++,

 ψ[[i, 1]] = Avg[MarsDisk[[i, 1]]];

 ψ[[i, 2]] = Avg[MarsDisk[[i, 2]]]]

which gave no error message but when I executed ψ it printed all elements as {Avg[0.8725], Avg[0.5]}, etc. So neither way worked.

Can anyone show me a way to successfully quantify the averages for each column of information in my matrix?

$\endgroup$
7
  • $\begingroup$ I should add that I changed "Avg" to "Average" and the same exact issue came up $\endgroup$ Commented Aug 11, 2016 at 16:40
  • 1
    $\begingroup$ why don't you use 'All' instead of defining a loop? can you give an example of your matrix? for example use Average[a[[All,2]]] $\endgroup$
    – Siav Josep
    Commented Aug 11, 2016 at 16:43
  • $\begingroup$ I'm an amateur with this stuff and didn't know about using All. I tried Average[MarsDisk[[All,2]]] and it worked out just fine!! Thank you so much! $\endgroup$ Commented Aug 11, 2016 at 16:47
  • $\begingroup$ I'm glad that I helped :) Also if you use the 'Mean' function it gives the average of each column of your matrix. $\endgroup$
    – Siav Josep
    Commented Aug 11, 2016 at 16:49
  • $\begingroup$ If you ran the For loop, then at the end of that evaluation i will be 18. This could explain the error message from Part you are seeing. $\endgroup$ Commented Aug 11, 2016 at 16:51

1 Answer 1

2
$\begingroup$
 matrix = Table[RandomReal[], {n, 1, 17}, {i, 1, 3}]

 Mean[matrix[[All, 2]]]

and i get the result

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.