I have a matrix Z that is (5,6) and I would like to circshift only its odd rows by 1. Any advice on how that can be done? Thanks!
MATLAB: Circshift Odd Rows by one
circshiftodd rows
Related Solutions
>> X = [1;2;3;4;5;6];>> Y = bsxfun(@plus,X,0:10:40);>> Z = bsxfun(@plus,Y(:),0:20);>> ZZ = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
Yes, circshift is a bit slow on the GPU particularly when it has to shift both rowwise and columnwise, because that means it has to stride the entire array reading and writing out to new locations - the GPU isn't especially good at that in comparison to main memory. You can see the GPU performance overtake the CPU somewhere around arrays of size 1000x1000. On my Kepler card:
>> A = rand(1000); B = gpuArray(A);>> timeit(@()circshift(A,[10 10]))ans = 0.0018>> gputimeit(@()circshift(B,[10 10]))ans = 0.0020>> A = rand(5000); B = gpuArray(A);>> timeit(@()circshift(A,[10 10]))ans = 0.0266>> gputimeit(@()circshift(B,[10 10]))ans = 0.0053
I'm not sure if I can advise how to improve it without writing your own version. Usually if you want to shift the data in a simple way you can reimplement it as some indexing and concatenation operations, which may well turn out to be faster for your particular use case.
Nevertheless I'll treat this as a request to improve the performance of circshift on the GPU.
Best Answer