matlab - Windowed subtraction of vectors that are not same size and then finding the mean of results -
good day,
i have question want achieve without loop if possible. title says need windowed subtraction of vectors not same size , finding mean of results.
as example, let have vector = [2 3 4 5 6] , vector b = [1 2]. program have move window smaller numbers of elements (in example vector b) on bigger 1 (vector a) , make operations on way starts in first 2 elements in vector , make subtraction vector b , sum results , find mean. in example make calculation of subtraction 2-1 = 1, 3-2 = 1, summing results 1+1=2 , divide them 2 (because vector b size). final result 1. move window on second elements of vector (value 3 , 4 there, or index 2 , 3) , repeat process last elements of vector a. final result need vector c consist of elements [1 2 3 4] example. possible without looping because have data sets on 10k of size. in advance
i can solve 1 loop, iterating through "b" (two loops in example).
declare vectors (as columns! needed matlabs computations work)
a = [2 3 4 5 6]'; b = [1 2]';
declare matrix computed results. each column represents subtractions of elements in "a" 1 of elements in "b".
c = zeros(length(a)-length(b)+1,length(b)); k = 1:length(b) c(:,k) = a(k:length(a)-length(b)+k)-b(k); end
now sum elements in "c" row wise , divide length of "b" mean
result = sum(c,2)/length(b);
you can simplify exact example, generic solution vetors "a" , "b", "b" smaller vector.
Comments
Post a Comment