Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Subscript indices must either be real positive integers or logicals

Subject: Subscript indices must either be real positive integers or logicals

From: matt reister

Date: 18 Jan, 2010 09:08:03

Message: 1 of 14

I have a rather simple matlab script that I am recieving the following error on:
Subscript indices must either be real positive integers or logicals.

size = 10;
x = zeros(size,6);
for i = 0:size
   x(i,:) = [i (i +1) (i +2) (i + 3) (i + 4) (i + 5) ];
end

I know the reason I aym recieving the error is because I am using i in the index of the x matrix, but it seems like i should be able to do this.... Does anyone know of a way the index a given row in a for loop like that?

Subject: Subscript indices must either be real positive integers or logicals

From: Oleg Komarov

Date: 18 Jan, 2010 09:36:02

Message: 2 of 14

"matt reister"
> I have a rather simple matlab script that I am recieving the following error on:
> Subscript indices must either be real positive integers or logicals.
>
> size = 10;
> x = zeros(size,6);
> for i = 0:size
> x(i,:) = [i (i +1) (i +2) (i + 3) (i + 4) (i + 5) ];
> end
>
> I know the reason I aym recieving the error is because I am using i in the index of the x matrix, but it seems like i should be able to do this.... Does anyone know of a way the index a given row in a for loop like that?

First of all don't use 'i' but 'ii'. Try to look for 'i' in the help.
Second a matrix is indexed for all elements starting from 1 to end. So the element 0 doesn't exist.
Third: 'i (i +1)' is not a valid expression in matlab. If you're trying to multiply, use the *.
For i = 0 your expression returns 0 (if * is assumed)
so why t start a loop from 0 at all?

And to avoid any loop:
[x,y] = meshgrid(0:5);
x = x+y;
Out = prod(x,2);

Oleg

Subject: Subscript indices must either be real positive integers or logicals

From: Oleg Komarov

Date: 18 Jan, 2010 09:45:19

Message: 3 of 14

"Oleg Komarov"
>
> And to avoid any loop:
> [x,y] = meshgrid(0:5);
> x = x+y;
> Out = prod(x,2);
>
> Oleg
Typo:
replace [x,y] = meshgrid(0:5); with
[x,y] = meshgrid(0:5,0:10);

Oleg

Subject: Subscript indices must either be real positive integers or logicals

From: Steve Amphlett

Date: 18 Jan, 2010 09:50:04

Message: 4 of 14

"matt reister" <mattreister@hotmail.com> wrote in message <hj18dj$8a3$1@fred.mathworks.com>...
> I have a rather simple matlab script that I am recieving the following error on:
> Subscript indices must either be real positive integers or logicals.
>
> size = 10;
> x = zeros(size,6);
> for i = 0:size
> x(i,:) = [i (i +1) (i +2) (i + 3) (i + 4) (i + 5) ];
> end
>
> I know the reason I aym recieving the error is because I am using i in the index of the x matrix, but it seems like i should be able to do this.... Does anyone know of a way the index a given row in a for loop like that?


Sadly, ML doesn't support zero-based indexing. x(0,:) isn't allowed. What a shame.

Subject: Subscript indices must either be real positive integers or logicals

From: Oleg Komarov

Date: 18 Jan, 2010 10:44:03

Message: 5 of 14

"Steve Amphlett"
> > I have a rather simple matlab script that I am recieving the following error on:
> > Subscript indices must either be real positive integers or logicals.
> >
> > size = 10;
> > x = zeros(size,6);
> > for i = 0:size
> > x(i,:) = [i (i +1) (i +2) (i + 3) (i + 4) (i + 5) ];
> > end
> >
> > I know the reason I aym recieving the error is because I am using i in the index of the x matrix, but it seems like i should be able to do this.... Does anyone know of a way the index a given row in a for loop like that?
>
>
> Sadly, ML doesn't support zero-based indexing. x(0,:) isn't allowed. What a shame.
Steve,
why it's a shame? (I use just Matlab and T-SQL so i can't find any good reason to be asham for :))
Oleg

Subject: Subscript indices must either be real positive integers or logicals

From: Matt J

Date: 18 Jan, 2010 12:37:03

Message: 6 of 14

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <hj1asc$h12$1@fred.mathworks.com>...

>
> Sadly, ML doesn't support zero-based indexing. x(0,:) isn't allowed. What a shame.


Although, I do show how you can construct a zero-based indexable array here:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/266512#696653

Subject: Subscript indices must either be real positive integers or logicals

From: Steve Amphlett

Date: 18 Jan, 2010 13:51:03

Message: 7 of 14

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <hj1e1j$8vb$1@fred.mathworks.com>...
> "Steve Amphlett"
> > > I have a rather simple matlab script that I am recieving the following error on:
> > > Subscript indices must either be real positive integers or logicals.
> > >
> > > size = 10;
> > > x = zeros(size,6);
> > > for i = 0:size
> > > x(i,:) = [i (i +1) (i +2) (i + 3) (i + 4) (i + 5) ];
> > > end
> > >
> > > I know the reason I aym recieving the error is because I am using i in the index of the x matrix, but it seems like i should be able to do this.... Does anyone know of a way the index a given row in a for loop like that?
> >
> >
> > Sadly, ML doesn't support zero-based indexing. x(0,:) isn't allowed. What a shame.
> Steve,
> why it's a shame? (I use just Matlab and T-SQL so i can't find any good reason to be asham for :))
> Oleg

It's a well trodden debate, not worth bringing up yet again. The CSSM archives are full of it. I've put up with it for 20+ years now, but still get bitten when people hand me ML prototypes to convert into real code.

Subject: Subscript indices must either be real positive integers or logicals

From: Matt J

Date: 18 Jan, 2010 14:32:02

Message: 8 of 14

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <hj1p07$kva$1@fred.mathworks.com>...

> It's a well trodden debate, not worth bringing up yet again. The CSSM archives are full of it. I've put up with it for 20+ years now, but still get bitten when people hand me ML prototypes to convert into real code.
=================

I think even with zero-based indexing, you have bigger hassles making that conversion: the absence of for-loops, the preponderance of unnecessary logical masks, etc...

Subject: Subscript indices must either be real positive integers or logicals

From: Steve Amphlett

Date: 18 Jan, 2010 14:54:04

Message: 9 of 14

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hj1rd2$qn1$1@fred.mathworks.com>...
> "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <hj1p07$kva$1@fred.mathworks.com>...
>
> > It's a well trodden debate, not worth bringing up yet again. The CSSM archives are full of it. I've put up with it for 20+ years now, but still get bitten when people hand me ML prototypes to convert into real code.
> =================
>
> I think even with zero-based indexing, you have bigger hassles making that conversion: the absence of for-loops, the preponderance of unnecessary logical masks, etc...

A moot point now. I spent a few days upgrading my C++ Matrix class to be either zero or one based. So ML code can be folded in line by line.

Subject: Subscript indices must either be real positive integers or logicals

From: Jan Simon

Date: 18 Jan, 2010 17:17:20

Message: 10 of 14

Dear Oleg!

> > x(i,:) = [i (i +1) (i +2) (i + 3) (i + 4) (i + 5) ];

> Third: 'i (i +1)' is not a valid expression in matlab. If you're trying to multiply, use the *.

BTW: '[i (i + 1)]' is valid because there is a space between "i" and "(". It is equivalent to:
  [i, i + 1]
I personally avoid vector notations with omitted separators, because they are susceptible for bugs. Compare:
 [i i]
 [i i + 1]
 [i i +1 ] % no space between + and 1
 [i i+ 1]
 [i i+1]
An even better:
 [i i...
+1] % to be true: I'm not sure if this is [1x2] or [1 x 3] vector!
  [1
    2]
  [1 ...
    2]
  [1, ...
    2]

Some of these notations procude different results when parsed by Matlab 5.3 - but I forgot which one. It found this so scary, that I decided to insert commas and semicolons whenever it is allowed.

Have fun, Jan

Subject: Subscript indices must either be real positive integers or logicals

From: matt reister

Date: 18 Jan, 2010 21:48:04

Message: 11 of 14

"matt reister" <mattreister@hotmail.com> wrote in message <hj18dj$8a3$1@fred.mathworks.com>...
> I have a rather simple matlab script that I am recieving the following error on:
> Subscript indices must either be real positive integers or logicals.
>
> size = 10;
> x = zeros(size,6);
> for i = 0:size
> x(i,:) = [i (i +1) (i +2) (i + 3) (i + 4) (i + 5) ];
> end
>
> I know the reason I aym recieving the error is because I am using i in the index of the x matrix, but it seems like i should be able to do this.... Does anyone know of a way the index a given row in a for loop like that?

Thanks alll.... its was the zero index that was all.... still learning

Subject: Subscript indices must either be real positive integers or logicals

From: Oleg Komarov

Date: 18 Jan, 2010 22:41:05

Message: 12 of 14

"Jan Simon"
> Dear Oleg!
>
> > > x(i,:) = [i (i +1) (i +2) (i + 3) (i + 4) (i + 5) ];
>
> > Third: 'i (i +1)' is not a valid expression in matlab. If you're trying to multiply, use the *.
>
> BTW: '[i (i + 1)]' is valid because there is a space between "i" and "(". It is equivalent to:
> [i, i + 1]
Jan,
if you haven't pointed it out i wouldn't have noticed! I agree with, in fact i prefer using some more commas than making these kind of mistakes.

To Matt,
you can still avoid the loop if you just need to concatenate.
Just remove the line with prod command and you'll have an incremental matrix (on both directions).

Oleg

Subject: Subscript indices must either be real positive integers or logicals

From: Keith Yushkov

Date: 11 Nov, 2010 10:49:03

Message: 13 of 14


> why it's a shame? (I use just Matlab and T-SQL so i can't find any good reason to be asham for :))
> Oleg

You have a very unique skill set!

Subject: Subscript indices must either be real positive integers or logicals

From: Keith Yushkov

Date: 11 Nov, 2010 11:10:06

Message: 14 of 14

> > And to avoid any loop:
> > [x,y] = meshgrid(0:5);
> > x = x+y;
> > Out = prod(x,2);
> >
> > Oleg
> Typo:
> replace [x,y] = meshgrid(0:5); with
> [x,y] = meshgrid(0:5,0:10);
>
> Oleg

Which of the following are more efficient in matlab? Does matlab do any compiling or pre-processing?

Error(k,1) = abs(real(V(k))-real(V(k+1)));
Error(k,2) = abs(imag(V(k)) - imag(V(k+1)));
    
Error(k:2) = [ abs(real(V(k))-real(V(k+1))) abs(imag(V(k)) - imag(V(k+1))) ];

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us