user899638
user899638

Reputation: 31

Subscript indices must either be real positive integers or logicals error with mean function in matlab

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!

Upvotes: 3

Views: 10557

Answers (2)

Lavya 'Orion'
Lavya 'Orion'

Reputation: 78

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

Upvotes: 0

Amro
Amro

Reputation: 124563

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...

Upvotes: 12

Related Questions