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

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 Commenti

Matt J
Matt J il 19 Mag 2014
Modificato: Matt J il 19 Mag 2014
Wow! Pretty disturbing. Interestingly, I find that it goes away when I set a breakpoint at the end of the code (R2013b). Here's an image of the result just for other peoples' reference.
I think part of the reason the bug might have gone undetected is that it is unusual, when dealing with vectors, to use more than 1 subscript. Normally, you would just do this,
a=a-a(1)
which does not produce the problem.
Interesting. It does go away if you use instead
b = a - a(1); a = a - a(1);
I also see that in Matt's case, the break happened at t = 256 or so from the plot. In my case, the break was at the 1025th element.
find(a ~= b,1,'first')
ans =
1025
format long g
a(1025)-b(1025)
ans =
0.710000000000001
This may be due to something like the number of processors, or some other CPU issue. (I'm running a MAC, 2014a.)
Try this:
a = 0.01*transpose(1:66536) + 0.7; t = transpose(1:66536) - 1;
b = a - a(1,:); a = a - a(1,:);
plot(t,a-b)
Anyone have a Pentium machine to try this on? :)
I see a lot of code that uses an unnecessary unit subscript posted here.
Just to check, writing a(:,1) works as expected.
Another oddity is that it is the first 256 values that are kept -- looks like a word size issue somehow, maybe?
This should be reported as a bug; it's also reproducible in R2012b but is not present in R12 (ca 2000). Optimizer problem, maybe?
I see the break at 257 on 2013b.
I see it at 1025 on 2008a and 2010a.
(Windows)
@John I checked to see and the problem occurred here at exactly element 256 as well...old Pentium, R2012b. As noted, I checked an earlier release (R12) and it didn't show a problem. Also reversing the subscripts didn't cause a problem (different answer, but expected one).
On Windows R2012b, the long series also had the blip occur at 257 and remained from there on. No break in the horizontal line.
I've submitted a bug report.
Commenting to track this issue, because Answers has no 'Watch List' functionality right? :)
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');
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 ;-))

Accedi per commentare.

 Risposta accettata

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 Commento

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...

Accedi per commentare.

Più risposte (1)

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

Categorie

Richiesto:

Tim
il 19 Mag 2014

Commentato:

dpb
il 20 Mag 2014

Community Treasure Hunt

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

Start Hunting!

Translated by