Matlab:按顺序重复每列n次 [英] Matlab: repeat every column sequentially n times
问题描述
我几乎是初学者,因此很可能可以用一种简单的方式来做我想做的事情. 我有一个121x62矩阵,但我需要将其扩展到121x1488,因此每一列都必须重复24次.例如,将其转换为:
I'm pretty much beginner so it's probably possible to do what I want in a simple way. I have a matrix 121x62 but I need to expand it to 121x1488 so every column has to be repeated 24 times. For example, transform this:
2.2668 2.2667 2.2667 2.2666 2.2666 2.2666
2.2582 2.2582 2.2582 2.2582 2.2581 2.2581
2.283 2.283 2.283 2.2829 2.2829 2.2829
2.2881 2.2881 2.2881 2.2881 2.2881 2.288
2.268 2.268 2.2679 2.2679 2.2678 2.2678
2.2742 2.2742 2.2741 2.2741 2.2741 2.274
对此:
2.2668 2.2668 2.2668 and so on to 24th 2.2667 2.2667 and again to 24x
2.2582 2.2582 2.2582 ...
每列.
我试图用这些值创建一个向量,然后用vec2mat进行转换,好了,我有121x1488矩阵,但按行重复:
I've tried to create a vector with these values and then transform with vec2mat and ok I have 121x1488 matrix but repeated by rows:
2.2668 2.2668 2.2668 2.2668 2.2668 2.2668 ... 2.2582 2.2582 2.2582 2.2582 ...
如何按列进行操作?
推荐答案
假设您具有此简化的输入,并且要按顺序扩展列n
次:
Suppose you have this simplified input and you want to expand columns sequentially n
times:
A = [1 4
2 5
3 6];
szA = size(A);
n = 3;
有几种方法可以做到:
-
复制,然后重塑:
Replicate, then reshape:
reshape(repmat(A,n,1),szA(1),n*szA(2))
Kronecker产品:
Kronecker product:
kron(A,ones(1,n))
使用 FEX:expand()
:>
expand(A,[1 n])
从R2015a开始, repelem()
:>
repelem(A,1,n)
所有结果都相同:
ans =
1 1 1 4 4 4
2 2 2 5 5 5
3 3 3 6 6 6
这篇关于Matlab:按顺序重复每列n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!