调整矢量的步长和窗口大小

时间:2019-02-27 09:23:55

标签: matlab reshape

例如,我有一个向量

A = [1 2 3 4 5 6 7 8]

我想使用windowsize=4stepsize=2对它进行“重塑”以使其矩阵化,使得最终的矩阵为

b = [ 1   3   5;   
      2   4   6;   
      3   5   7;   
      4   6   8 ]

1 个答案:

答案 0 :(得分:4)

您可以设置索引矩阵,然后只需索引到 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="table-responsive"> <table class="table"> <thead> <tr> <th>1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> <th>Header 5</th> <th>Header 6</th> <th>Header 7</th> <th>Header 8</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> <td>longtextlongtextlongtext</td> <td>6</td> <td>7</td> </tr> </tbody> </table> </div> </div> </body> </html> ...

A

注意;在这种情况下,A = [1 2 3 4 5 6 7 8]; windowsize = 4; stepsize = 2; % Implicit expansion to create a matrix of indices idx = bsxfun( @plus, (1:windowsize).', 0:stepsize:(numel(A)-windowsize) ); b = A(idx); idx是相同的,但是您需要在最终索引步骤中假设b不仅仅是实际示例中的连续整数。