Create matrix by repeatedly overlapping a vector

前端 未结 4 2045
再見小時候
再見小時候 2021-01-19 20:52

I\'m having great difficulty coding the following in MATLAB: Suppose you have the following vector:

a   
b
c
d
e
f
g
h
...

Specifying an (e

4条回答
  •  再見小時候
    2021-01-19 21:33

    Here's a general way to do what you want:

    1) Calculate the appropriate window width (and corresponding shift)
    2) Determine the start indices of each column by iterating from 1 by the amount you want to shift the window each column, up to the final value. Make this a row vector.
    3) Use bsxfun to expand this to a matrix of indices.
    4) Use the indices to get the values from the original vector.

    vec = 1:17; #% original data vector
    num_windows = 3; #% specified number of windows
    possible_window_length = 1:length(vec);
    window_length = possible_window_length(find(possible_window_length +...
        (num_windows-1) * possible_window_length/2 < length(vec),1,'last'));
    window_shift = floor(window_length)/2;
    window_length = window_shift * 2; #% calculated window length
    max_final_start_index = (length(vec)-window_length+1);
    start_indices = 1:window_shift:max_final_start_index;
    inds = bsxfun(@plus,start_indices,(0:window_length-1)');
    soln = vec(inds); #% get the solution
    num_excluded_vals = max_final_start_index - start_indices(end)
    disp(soln);
    num_excluded_vals =  1
    disp(soln);
        1    5    9
        2    6   10
        3    7   11
        4    8   12
        5    9   13
        6   10   14
        7   11   15
        8   12   16
    

提交回复
热议问题