1

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

4

1 回答 1

2

使用kron

K = kron(X,Y)返回 X 和 Y 的 Kronecker 张量积。结果是一个大数组,由 X 的元素和 Y 的元素之间的所有可能的乘积形成。如果 X 是 m×n 且 Y 是 p×q,则kron(X,Y) 是 m*p×n*q。

在你的情况下:

kron(a,[1 1])

会给你你想要的

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

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

reshape([a'*[1 1]]',1,[])
于 2013-07-07T04:16:18.483 回答