I have a data of 512 bits, I want to slice it in 16 equal parts each of 32 bits. How can I do it in MATLAB using a for loop?
example:
inputdata=[1,0,1,0,0,0,0,0,1,1,1,0,0,1,0]
I want:
outpu1=[1,0,1,0,0] output2=[0,0,0,1,1] output3=[1,0,0,1,0]
inputdata=[1,0,1,0,0,0,0,0,1,1,1,0,0,1,0];
slices = 3; %// number of slices
slicelength = numel(inputdata)/slices;
kk=1;
for ii = 1:slices
slicedarray(ii,:) = inputdata(kk:ii*slicelength);
kk=ii*slicelength+1;
end
now slicedarray will contain your slices, with each row being one slice. You do not want the output variables like you asked for, because dynamic variable names are bad.
Vectorising stuff is faster in MATLAB, thus you can use reshape:
inputdata=[1,0,1,0,0,0,0,0,1,1,1,0,0,1,0];
slices = 3;
slicelength = numel(inputdata)/slices;
slicedarray(slices,slicelength)=0; %// initialise for speed
output = reshape(inputdata,[slices slicelength]);
In both cases the output is:
output =
1 0 0 1 0
0 0 0 1 1
1 0 1 0 0
Copyable for your benefit:
inputdata=rand(512,1);
slicelength = 16;
slices = numel(inputdata)/slicelength;
kk=1;
slicedarray(slices,slicelength)=0;
for ii = 1:slices
slicedarray(ii,:) = inputdata(kk:ii*slicelength);
kk=ii*slicelength+1;
end
output = reshape(inputdata,[slices slicelength]);