Why is b = a - a(1,:) not equal to a = a - a(1,:) where a is a column vector.

2 views (last 30 days)
A colleague was removing the offset of her capured data time vector (subtracting the first element from all elements such that the new time vector starts at time t = 0) when she encountered a problem. For a column vector a, the command a = a - a(1,:) only subtracts a(1,:) from some of the first elements.
Code to recreate the problem:
a = 0.01*transpose(1:2048) + 0.7;
t = transpose(1:2048) - 1;
b = a - a(1,:);
a = a - a(1,:);
plot(t,a,t,b);
legend('a','b');
  11 Comments
Sean de Wolski
Sean de Wolski on 19 May 2014
Edited: Sean de Wolski on 19 May 2014
Using the ':' rather and a normal colon works too as does using 1:end or turning off the jit.
a = 0.01*transpose(1:2048) + 0.7;
t = transpose(1:2048) - 1;
b = a - a(1,:);
a = a - a(1,':');
plot(t,a,t,b);
legend('a','b');
Cedric
Cedric on 19 May 2014
Edited: Cedric on 19 May 2014
Ditto bis.. emergency situation @ Mathworks.
Another funny behavior is that the following works
b = a - a(1,:);
c = a ;
a = a - a(1,:);
Well, and the following doesn't work
b = a - a(1792,:);
a = a - a(1792,:);
but
b = a - a(1793,:);
a = a - a(1793,:);
does work. Interestingly, 1792 = 2048 - 256 (last working element on the other side).
(Win64, R2013b - Sorry John, no Pentium, but I have a Z80 if you want ;-))

Sign in to comment.

Accepted Answer

Matt J
Matt J on 19 May 2014
From Tech Support:
I am writing in reference to your Service Request, Case #00977058 regarding 'Vectors with Multiple Subscripts'.
I was able to reproduce the issue. This seems to be a problem with the MATLAB Just in Time (JIT) accelerator which analyzes the code in the editor and makes optimizations. This works in debug mode since JIT is not active in debug mode.
I have notified our developers about this issue and they may consider addressing it in a future release of MATLAB.
As a workaround, you can turn of the JIT feature by the following command:
>> feature accel off
Now the results of 'a' and 'b' should be identical.
You can turn the JIT back on using the following command:
>> feature accel on
One other workaround is to use a(1) instead of a(1,:).
Please feel free to reply back, if you have any further queries in this regard. I will be happy to reopen the case and assist you.
Please preserve the Reference ID below in any further correspondence on this query. This will allow our systems to automatically assign your reply to the appropriate Case.
  1 Comment
dpb
dpb on 20 May 2014
I just love the "political correctness" (not) ...
...and they _may consider addressing it in a future release of MATLAB._
Confirmed silent bug and it only "may" be corrected...

Sign in to comment.

More Answers (1)

Tim
Tim on 20 May 2014
Thanks for all the quick replies. Certainly there is no reason to use a(1,:) rather than a(1) or a(1,1) for a column vector, and I assume the problem doesn't exist for m*n matrices or someone would have spotted it.
For reference, my break happens at element 256 on Windows R2012b.
Cheers, T

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!