Split a vector into 2 oscillating vectors?

1 view (last 30 days)
I have a .csv file with 1M datapoints, which I've turned into a simple column vector "V". I want to turn it into 2 seperate vectors that take 5k of the data points, back and forth.
So V1 takes 1:5000, 10001:15000, 20,001:25000, etc.
V2 takes 5001:10000, 15001:20000, etc.
Is there an easy way to do this other than typing out 100 sections for each variable?

Accepted Answer

Voss
Voss on 18 Jan 2022
Here is a way to do it. Demonstrating with smaller vectors so as to more easily verify the result. To use it on your column vector V, change N to 1e6 and block_size to 5000.
% N = 1e6;
% block_size = 5000;
N = 140;
block_size = 5;
V = (1:N).'; % some column vector with N elements
idx = (1:block_size).'+2*(0:N/block_size/2-1)*block_size;
V1 = V(idx(:));
V2 = V(idx(:)+block_size);
display([V1 V2]);
1 6 2 7 3 8 4 9 5 10 11 16 12 17 13 18 14 19 15 20 21 26 22 27 23 28 24 29 25 30 31 36 32 37 33 38 34 39 35 40 41 46 42 47 43 48 44 49 45 50 51 56 52 57 53 58 54 59 55 60 61 66 62 67 63 68 64 69 65 70 71 76 72 77 73 78 74 79 75 80 81 86 82 87 83 88 84 89 85 90 91 96 92 97 93 98 94 99 95 100 101 106 102 107 103 108 104 109 105 110 111 116 112 117 113 118 114 119 115 120 121 126 122 127 123 128 124 129 125 130 131 136 132 137 133 138 134 139 135 140

More Answers (1)

David Hill
David Hill on 18 Jan 2022
v1=zeros(1,500000);
v2=zeros(1,500000);
for k=1:100
v1((k-1)*5000+1:k*5000)=v((2*(k-1))*5000+1:(2*(k-1)+1)*5000);
v2((k-1)*5000+1:k*5000)=v((2*(k-1)+1)*5000+1:k*10000);
end

Tags

Community Treasure Hunt

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

Start Hunting!