Subset of an array with a moving window
이전 댓글 표시
I have the following code for computing structure functions. I have provided examples of "ULs" and "UTs" merely for simplicity.
ULs = rand(1,145); % called in from function
UTs = rand(1,145); % called in from function
nrmax = 145; % called in from function, I just happen to be using 145
for nr = 1:nrmax
Nts = numel(ULs); % number of elements
Umat_L = nan(Nts-nr,nr+1); % must be NaN, so zeros() won't work
Umat_T = nan(Nts-nr,nr+1); % must be NaN, so zeros() won't work
for i = 1:(Nts-nr) % this is
Umat_L(i,:)=ULs(i:i+nr); % the for loop
Umat_T(i,:)=UTs(i:i+nr); % I want to remove
end
end
The nested loop thus takes subsets ULs(1:1+nr), then ULs(2:2+nr), ULs(17:17+nr), etc. Is it possible to replicate this subsetting process without using a loop?
It would improve my code run time if I could remove the nested "for i = 1:(Nts-nr)". However, as I see it, there is no way around this because of the nature of the moving window. I tried the following substitution, but it didn't work.
i = (1:Nts-nr);
inr = i + nr;
Umat_L(i,:) = ULs(i:inr);
Umat_T(i,:) = UTs(i:inr);
댓글 수: 2
Rik
2021년 4월 16일
Many moving window operations can be replaced by movmean and other mov* functions, or by convolutions. The image processing toolbox also contains several useful functions.
Is this code representative of the result you actually want, or is there a further step in calculation?
Alex Nickerson
2021년 4월 16일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!