3

I have a giant data array where in every row I want to extract specific columns and then average the numbers I extract. This is my code:

for i=1:1000
    temp=data(i,:);
    index_data=temp([1,10,11,12,19]); %columns I want to extract
    data_final(i,1)=mean(index_data(~isnan(index_data)));
end

I get an error on the first iteration. The array that is extracted is

[NaN NaN NaN NaN 15.64]

And what I get when I type index_data(~isnan(index_data)) is 15.64, what I'd expect. However, I get an error of

??? Subscript indices must either be real positive integers or logicals.
Error in ==> mean(index_data(~isnan(index_data)))

Calling this from the command line yields the same error. However, if I try with the covariance function (cov) I don't the get the error. This seems really weird to me!

Thanks for your help!

user899638
  • 31
  • 1
  • 1
  • 2
  • Also see [this question](http://stackoverflow.com/questions/20054047/subscript-indices-must-either-be-real-positive-integers-or-logicals-generic-sol) for [the generic solution to this problem](http://stackoverflow.com/a/20054048/983722). – Dennis Jaheruddin Nov 27 '13 at 15:45

2 Answers2

12

I suspect that you have defined a variable mean with the same name as the built-in function. You can check using:

which mean

To solve the problem, delete the variable from memory: clear mean, then correct all references of this variable...

Amro
  • 123,847
  • 25
  • 243
  • 454
0

I had a similar problem. The error in my case was due to using mean as a variable name, which also happens to be a name of a function.

Since it was a mean, MATLAB considered the keyword mean as a variable name but not the function.

I hope my comment helps