-2

I have one 194-by-1 matrix. When I try to find its size, I get this message:

Subscript indices must either be real positive integers or logicals.

All values are positive and logical, what is the problem?

Eitan T
  • 32,660
  • 14
  • 72
  • 109
YSF
  • 47
  • 1
  • 2
  • 7
  • 3
    Can you post the part of code that triggers this error? – Eitan T May 09 '13 at 11:14
  • 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:43

3 Answers3

5

Is it possible that you accidentally override the size function?

>> which size

Should give this output

built-in (C:\ X X X \toolbox\matlab\elmat\size)

If you get something like

size is a variable

Then you override the function.

To ammend this you'll have to clear the variable

>> clear size

Now you can use the function

>> size( A )
Shai
  • 111,146
  • 38
  • 238
  • 371
2

Can you provide your code? It should work if you do it like this:

[rows cols] = size(A);

or

rows = length(A);
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
2

In MATLAB, size is a function that returns the dimensions of an array (a matrix, a cell array, etc...). However, MATLAB also supports overloading. When you call size(A) (assuming A is your matrix), the MATLAB interpreter first checks if there are overloaded variables/functions with the name size.

Apparently you have a variable named size, judging by the error message, so for MATLAB size(A) means that your trying to index into the matrix size with the subscript variable A. It seems that A has one or more zero elements, and since non-positive indices in MATLAB are forbidden, this triggers the aforementioned error.

The simplest solution is to use another name for your variable size so you can call the built-in function size without any issues.

Eitan T
  • 32,660
  • 14
  • 72
  • 109