Thread Subject: bug with indices ??

Subject: bug with indices ??

From: Juliette Salexa

Date: 13 Jul, 2009 18:22:01

Message: 1 of 7

Is this a bug with matlabs subscripting ?? shouldn't the last command do a combination of the second and the third ??

c(1:3,1)='a'
c =
a
a
a

>> c(1,2:4)='(i)'
c =
a(i)
a
a

>> c(:,2)='('
c =
a(i)
a(
a(

>> c(:,2:4)='(i)'
??? Assignment has fewer non-singleton rhs dimensions than non-singleton
subscripts


This also happens if I predefine the size of c to be 3,4

Subject: bug with indices ??

From: Matt Fig

Date: 13 Jul, 2009 18:35:04

Message: 2 of 7

Try this instead:


c(:,2:4) = repmat('(i)',3,1)

Subject: bug with indices ??

From: Bruno Luong

Date: 13 Jul, 2009 18:40:18

Message: 3 of 7


> >> c(:,2:4)='(i)'
> ??? Assignment has fewer non-singleton rhs dimensions than non-singleton
> subscripts

Left hand side c(:,2:4) is (3 x 3) matrix (because c has three rows)
Right hand side '(i)' is (1 x 3) char matrix.

The error is then predictable since the sizes do not match.

Bruno

Subject: bug with indices ??

From: Juliette Salexa

Date: 13 Jul, 2009 19:00:21

Message: 4 of 7

Thanks Bruno, I'd heard of this repmat function but didn't realize its use until now.
I still don't understand this subscript assignment problem though:

> > >> c(:,2:4)='(i)'
> > ??? Assignment has fewer non-singleton rhs dimensions than non-singleton
> > subscripts
>
> Left hand side c(:,2:4) is (3 x 3) matrix (because c has three rows)
> Right hand side '(i)' is (1 x 3) char matrix.
>

True, but in the example above it:

c(:,2)='('
c =
a(
a(
a(

c(:,2) is a 3x1 matrix and the right side '(' is a (1x1) char matrix. And that works perfectly .. although it doesn't seem to work in the above case :(

Subject: bug with indices ??

From: Bruno Luong

Date: 13 Jul, 2009 19:09:03

Message: 5 of 7

"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message <h3g085$4bp$1@fred.mathworks.com>...

>
> c(:,2) is a 3x1 matrix and the right side '(' is a (1x1) char matrix. And that works perfectly .. although it doesn't seem to work in the above case :(

The later do works because "scalar expansion" (scalar is 1x1).

http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f2-14896.html#f2-15099

There is no such rule for non-scalar, you need to make REPMAT manually, as Matt indicates.

Bruno

Subject: bug with indices ??

From: Steven Lord

Date: 13 Jul, 2009 21:56:09

Message: 6 of 7


"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message
news:h3fu09$29a$1@fred.mathworks.com...
> Is this a bug with matlabs subscripting ?? shouldn't the last command do a
> combination of the second and the third ??
>
> c(1:3,1)='a'
> c =
> a
> a
> a
>
>>> c(1,2:4)='(i)'
> c =
> a(i)
> a
> a

This works because you're trying to store a 1-by-3 array '(i)' into a 1-by-3
piece of c.

>>> c(:,2)='('
> c =
> a(i)
> a(
> a(

For this, you're trying to store a 1-by-1 array '(' into a 3-by-1 piece of
c, so you would expect it not to work since they're not the same size.
However, certain operations in MATLAB treat scalar inputs a little
differently, and apply a procedure known as "scalar expansion" to the scalar
input. For example, if you do something like:

x = eye(5) + 1

Technically, you shouldn't be able to do that by the rules of matrix
addition, since 1 isn't the same size as eye(5). However, most of the time
(*) what you want is to have 1 added to each element of eye(5). Rather than
force you to write:

x = eye(5) + repmat(1, [5 5])

which would require additional memory, MATLAB internally "expands" 1 to be
the same size as eye(5) [without actually using the memory creating that
expanded matrix would require] and performs the addition.

>>> c(:,2:4)='(i)'
> ??? Assignment has fewer non-singleton rhs dimensions than non-singleton
> subscripts

In this case, you're trying to store a 1-by-3 array into a 3-by-3 piece of
c, and that's not allowed. There's no scalar expansion involved, and the
pieces aren't the same size, so you (correctly) receive an error.

--
Steve Lord
slord@mathworks.com

(*) One exception that comes to mind is if you're evaluating a polynomial on
a matrix.

x = [1 2;3 4];
y = 3*x^3+2*x^2+1*x^1+5

is not the same as

x = [1 2;3 4];
y = 3*x^3+2*x^2+1*x^1+5*x^0

Subject: bug with indices ??

From: Juliette Salexa

Date: 13 Jul, 2009 22:25:21

Message: 7 of 7

Thank you Steven Lord, that was very helpful!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
no_bug Matt Fig 13 Jul, 2009 14:42:09
rssFeed for this Thread

Contact us at files@mathworks.com