复制向量并将每个副本向下移动 1 行,无需 for 循环 [英] Replicate vector and shift each copy by 1 row down without for-loop
本文介绍了复制向量并将每个副本向下移动 1 行,无需 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想复制一个向量 N
次来创建一个矩阵,每个副本向下移动 1 行.请参见图像(第一列是向量 1 到 5).如果不用 for 循环也能实现,那就太好了.
I would like replicate a vector N
times to create a matrix with each copy shifted 1 row down. See image (first column is the vector 1 to 5). It would be great if this can be achieved without using for loop.
到目前为止能够做到这一点 repmat(my_vector, 1, 5)
来创建一个 N x 5 矩阵.
So far was able to to do this repmat(my_vector, 1, 5)
to create an N x 5 matrix.
推荐答案
a = [1 2 3 4 5]
out = tril( toeplitz(a) )
或
out = toeplitz(a, a*0)
%// out = toeplitz(a, zeros(size(a)) ) %// for large arrays
或者如果你不介意一些快乐的翻转:
or if you don't mind some happy flipping:
out = flipud( hankel( flipud(a(:)) ) )
这篇关于复制向量并将每个副本向下移动 1 行,无需 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文