1

I have just recently started using MATLAB as it's pretty good for machine learning and the like.

Currently, I am working on some type of classification which is pretty long-winded and complicated if I tried to explain everything I am trying to accomplish therefore I will just state the exact code giving me problems.

So, I am given a 1010 x 1764 single type matrix by some function. say the matrix is called train_examples_2_2 as you can see on the right-hand side of the screenshot below.

enter image description here

As you can also see from the screenshot above (on the right-hand side), the calls to mean and std:

mean = mean(train_examples_2_2)
std = std(train_examples_2_2)

Yield the correct results.

However, when I run the same code several times sometimes I get an error on the line mean = mean(train_examples_2_2) stating:

Array indices must be positive integers or logical values.

The exact code I am concerned with is:

mean = mean(train_examples_2_2) % <----- error appears here
std = std(train_examples_2_2)
for i=1:size(train_examples_2_2,1)
   train_examples_2_2(i,:) = train_examples_2_2(i,:) - mean;
   train_examples_2_2(i,:) = train_examples_2_2(i,:) ./ std;
end
% end of standardisation process

where train_examples_2_2 is provided by some function that I did not create nor can modify.

According to the MATLAB documentation:

If A is a matrix, then mean(A) returns a row vector containing the mean of each column.

which is what I get the first time I run the code upon opening Matlab but after that, it yields the aforementioned error.

I am using MATLAB R2018b.

I'm I making a simple mistake or could this possibly be a bug?

Thanks for taking the time to help out.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 3
    You've overshadowed the function `mean()` by naming your variable `mean`. Rename the variables `mean` and `std` and it should fix your problem. – beaker Mar 07 '19 at 22:34

1 Answers1

3

unlike let's say python you shouldn't/can't/mussn't re-define function names or default variables.

mean = mean(train_examples_2_2) % <----- error appears here

matlab doesn't distinguish between the callable mean() function and the variable ```mean``. especially confusing since indexing and calling sth is done by using round brackets.

So....?

call your variable sth. other than mean. mean_ will already do the trick.

erkandem
  • 136
  • 1
  • 7