0

I generated random values using following function :

 P = floor(6*rand(1,30)+1)

Then, using T=find(P==5), I got values where outcome is 5 and stored them in T. The output was :

T =

   10   11   13   14   15   29

Now, I want to calculate the mean value of T using mean(T) but it gives me following error :

error: mean(29): out of bound 1 (dimensions are 1x1) (note: variable 'mean' shadows function)

What I am trying to do is to model the outcomes of a rolling a fair dice and counting the first time I get a 5 in outcomes. Then I want to take mean value of all those times.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 4
    `note: variable 'mean' shadows function` -- you have a function and variable both named **mean** -- change the variable name to something else. – David C. Rankin Jun 12 '21 at 07:03

1 Answers1

2

Although you don't explicitly say so in your question, it looks like you wrote

mean = mean(T);

When I tried that, it worked the first time I ran the code but the second and subsequent times it gave the same error that you got. What seems to be happening is that the first time you run the script it calculates the mean of T, which is a scalar, i.e. it has dimensions 1x1, and then stores it in a variable called mean, which then also has dimensions 1x1. The second time you run it, the variable mean is still present in the environment so instead of calling the function mean() Octave tries to index the variable called mean using the vector T as the indices. The variable mean only has one element, whose index is 1, so the first element of T whose value is different from 1 is out of bounds. If you call your variable something other than mean, such as, say, mu:

mu = mean(T);

then it should work as intended. A less satisfactory solution would be to write clear all at the top of your script, so that the variable mean is only created after the function mean() has been called.

Howard Rudd
  • 901
  • 4
  • 6