How can I vectorize this for loop?
1 view (last 30 days)
Show older comments
I am having a hard time vectorizing this for loop. In this example I am trying to fill a matrix from a data vector (it is numbered 1 through 96 for testing out the script, but would eventually contain real data). Each matrix column covers a different range of indices from the data vector. Thanks,
% These are example values for testing script. They can change, but
% are tied together so do not change values or datamatrix will not be resolved
BS=16; % this value is specific to the size of the DataVector; just listed here for this example, do not change or matrix will not resolve
ptsOL=8; % this value is specific to the size of the DataVector; just listed here for this example, do not change or matrix will not resolve
NoBlocks=11; % this value is specific to the size of the DataVector; just listed here for this example, do not change or matrix will not resolve
DataVector=1:96; % Data - numbered 1 through 96 for testing out the script, but would eventually contain real data, do not change or matrix will not resolve
%%%% How to vectorize the following for loop
DataMatrix=zeros(BS,NoBlocks); % initialize matrix
for i=1:NoBlocks
DataMatrix(:,i)=DataVector((i-1)*(BS-ptsOL)+1:(i-1)*(BS-ptsOL)+BS); % place all data blocks into separate consecutive columns
end
0 Comments
Accepted Answer
TADA
on 9 Sep 2021
irow = (0:(NoBlocks-1))*ptsOL+1;
icol = (0:BS-1)';
idxMat = irow+icol;
DataMatrix = DataVector(idxMat);
1 Comment
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!