交织向量 [英] Interweaving vectors

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

问题描述

我想在 MATLAB 中交织两个向量.事实上,我实际上只是想在每个元素之间添加一个零,但我想我会以这样一种方式提出问题,以便我学习如何将其应用于其他情况.

I would like to interweave two vectors in MATLAB. In fact, I'd actually just like to add a zero between each element, but I figured I'd ask the question in such a way that I'd learn how to apply this to other situations.

我的具体应用:我想采用一个向量(例如 [1 2 3])并输出 [0 1 0 2 0 3].

My specific application: I'd like to take a vector (e.g. [1 2 3]) and output [0 1 0 2 0 3].

更广泛的问题:我将如何使用两个不同的向量来做到这一点,例如[1 2 3][9 8 7] 交织产生[9 1 8 2 7 3].

The wider question: How would I do this with two different vectors, e.g. [1 2 3] and [9 8 7] interweaving to produce [9 1 8 2 7 3].

非常感谢您对上述任一或两个问题的任何帮助.

Any help greatly appreciated, in either or both of the above questions.

推荐答案

这里有一些代码可以完成你想要的:

Here's some code that will accomplish what you want:

nums   = rand(1,3)
output = zeros(1,2*numel(nums));
output(2:2:end) = nums

输出如下:

nums =

    0.9134    0.6324    0.0975


output =

         0    0.9134         0    0.6324         0    0.0975

按照其他两个答案的风格,您可以通过以下方式获得所需的零输出:

Following the style of the other two answers, you could get your desired zeros output with the following:

nums = rand(1,3);
reshape([zeros(size(nums));nums],1,[])

显然,nums 应该替换为您想要使用的向量.如前所述,在调用 reshape 之前,您应该确保它是一个行向量.

Obviously, nums should be replaced with the vector you'd like to use. As mentioned, you should make sure it's a row vector before calling reshape.

这篇关于交织向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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