在Matlab中我有以下向量:
a = [ 1 2 3 4 5 6 ];
我想构建一个矩阵,每行快速生成1个元素.输出矩阵应如下所示:
A = 1 2 3 2 3 4 3 4 5 4 5 6
哪种方法最快?提前告诉你!
简单的矢量化解决方案:
a = [ 1 2 30 4 15 6 ] m = 4; n = 3; %// If you want the last element of a to be the bottom right %// element of your output then n must equal numel(a)-(m-1) [r,c] = ndgrid(0:(m-1), 1:n); a(r+c) ans = 1 2 30 2 30 4 30 4 15 4 15 6