5

I have big code and i noticed a bug in my code without any syntax error. I describe it using the following example:

I=sym(eye(3)); %I is Identity 3by3
a=sym(zeros(5,1)); %a is column matrix

then :

a(1)=I;

which should give an error but the result is:

a=

 1
 0
 0
 0
 0

it means that matlab assigns a 3x3 matrix into an element of a!!!

what is wrong?

I tried the same but know using I and a both of double type now it gives an error which is correct.

milad
  • 169
  • 1
  • 2
  • 10
  • 1
    It's assigning the first **element** of `I` to the first element of `a`. instead of throwing an error as it would with a different class. – RTL May 22 '14 at 08:32
  • 1
    looking at the `sim.subsasgn` ,the function used for the syntax `a(1)=I` when dealing with sym, this is expected as it loops through the indices seemingly assigning one value to one value for example `a(1:5)=I` will give `a=[1,0,0,0,1]'`, **NOTE:** the help documentation (in 2013a) doesn't mention this different behaviour, possibly worth a [bug report](http://www.mathworks.com/support/bugreports/)? – RTL May 22 '14 at 08:49
  • @RTL but a is initilizes as zeros, so maybe initializing a in some other way can solve the problem? no idea, jus suggesting – Ander Biguri May 22 '14 at 08:53
  • @AnderBiguri True, depending on the usage of `a`... @milad if you want to store a 3x3 sym in a something of size 5x1 you could use a cell array: for example `a=cell(5,1)` `a{1}=I` – RTL May 22 '14 at 09:09
  • @RTL of course it is possible to use `cell` but i think this maybe a little bug. – milad May 22 '14 at 11:35
  • I'm fairly sure the behaviour is undocumented and the fact it doesn't work as it would with other classes leads me to think it is unintended so I agree on the bug... unless the documentation changes – RTL May 22 '14 at 11:45

1 Answers1

1

--Converted from comments---

What's the problem

In general attempting to assign something of size 3x3 to a smaller array will cause Matlab throw an error. however with sym the following happens:

I=sym(ones(3));
a=sym(zeros(5,1))
n=3
a(1:n)=I

a =

 1
 1
 1
 0
 0

For some reason with variables of class sym no error is caused. If more elements are given in I than will fit in the n assigned positions of a. It will assign the first n values of I to a

Why

In the subsasgn method for in the classdef of sim (subsasgn being the method used for the syntax a(1)=I) no check on the size is present (not entirely true as if a is an empty sym array and error is caused) The function iterates through the n locations in a assigning the first n values of I to each individual position in a.
For example the code above it is equivalent of performing a(1:n)=I(1:n), which would be the command to generate this behaviour with double.

Is this intended?

Not a clue!
The help documentation doesn't mention this different behaviour so I assume it is a bug, a service request has been put in for either documentation or fixing.

What can be done

Be careful - sorry but that is all I've got to avoid this problem

EDIT -- Support request was answered ---

Yes you are right; I apologize for the inconvenience this unexpected behavior might cause. This indeed appears as being inconsistent with base MATLAB behavior. Thank you for bringing this to our attention as this behavior should be documented (if not issuing a warning). I will create a relevant documentation enhancement today.

...seems like it is soon to be not a bug but documented behaviour

RTL
  • 3,577
  • 15
  • 24