MATLAB:复制向量的每个元素? [英] MATLAB: Duplicate each element of a vector?

查看:192
本文介绍了MATLAB:复制向量的每个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是MATLAB和该网站的新手.我尝试搜索此问题,但无济于事(因此,如果这最终成为之前已经在此处提出的问题,我深表歉意).在课堂上,我们被分配了一个问题,具有以下描述:对于n维向量X,该函数应返回另一个2n维,其中每个元素重复两次.例如:如果a = [2 3 4 5],使用该函数后,a = [2 2 3 3 4 4 5 5]; 它应该可以使用任何随机大小的向量.

I'm new to MATLAB and this website as well. I tried searching for this question, but to no avail (so I apologize if this ends up being a questions which has already been asked here before). In class, we were assigned a problem with the following description: "For an n-dimensional vector X, the function should return another 2n-dimension where each element is repeated twice. For example: if a=[2 3 4 5], after using the function, a=[2 2 3 3 4 4 5 5];" It should work with a vector of ANY random size.

非常感谢您的帮助! 谢谢

Your help is really appreciated! Thanks

推荐答案

使用 kron :

K = kron(X,Y)返回X和Y的Kronecker张量积.结果是通过取X元素和Y的元素之间的所有可能乘积形成的大数组.如果X为m乘n,Y为p -by-q,则kron(X,Y)为m * p-by-n * q.

K = kron(X,Y) returns the Kronecker tensor product of X and Y. The result is a large array formed by taking all possible products between the elements of X and those of Y. If X is m-by-n and Y is p-by-q, then kron(X,Y) is m*p-by-n*q.

您的情况:

kron(a,[1 1])

会给您您想要的东西

您的问题的一些替代答案:

Some alternatives answers for your question:

reshape([a ; a],1,[])

reshape([a'*[1 1]]',1,[])

这篇关于MATLAB:复制向量的每个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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