Problem 963. We love vectorized solutions. Problem 1 : remove the row average.
Given a 2-d matrix, remove the row average from each row. Your solution MUST be vectorized. The solution will be tested for accuracy AND speed. Any for loops will make the solution too slow to pass the speed test.
Example:
a = [ 1 2 ; 3 5 ]; b = remove_average_vectorized(a); % b is [ -0.5 0.5 ; -1 1 ];
Solution Stats
Problem Comments
-
9 Comments
Please modify the fourth test suite so it reports the duration when the assert fails. My tests say I should pass but Cody is exceeding the time. The method to output information on a failed assert is: assert(duration<2,sprintf('Time used %2.3f\n',duration));
thx
The other thing to do is to remove the ";" from the duration=toc(timer) line. This will allow everyone to see how fast the successful routines take to execute. My self-test times are < 500msec but I don't know Cody's speed. thx
Thanks Richard, your suggestions have been implemented.
I am hoping to stimulate a change to cody where some problems (or all) problems are graded on performance not size. A low size often means an unreadable solution, and people have found enough tricks to make size an irrelevance, anyway.
As per Solution 1539945, the Test Suite has been corrupted for me. For me the first line of the Test Suite appears to read "%%x = [ 0 1 ; 2 3 ] ;", and the correct inputs are not supplied. This may be the same Cody bug that previously affected Problem 970 (& perhaps Problem 1319) and still affects Problem 753.
Timing aspect is neat.
The solution with for is able to solve this problem currently within time. When requesting performance, you should measure the time dynamically, or else you will be forced to adjust the threshold as the years go by and computers evolve.
You should make the timing test dynamic. The current optimally vectorized solution is about 7 or 8 times faster than the default looped solution. You could add a test that confirms at least a 75% time reduction.
This was pretty easy...
Solution Comments
Show commentsProblem Recent Solvers826
Suggested Problems
-
27700 Solvers
-
433 Solvers
-
450 Solvers
-
Number of 1s in a binary string
9242 Solvers
-
933 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!