Matlab:按顺序重复每一列n次 [英] Matlab: repeat every column sequentially n times

查看:52
本文介绍了Matlab:按顺序重复每一列n次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎是初学者,所以可能可以用简单的方式做我想做的事.我有一个 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;

有几种方法可以做到这一点:

There are few ways to do that:

  • 复制,然后重塑:

  • Replicate, then reshape:

reshape(repmat(A,n,1),szA(1),n*szA(2))

  • 克罗内克产品:

  • 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆