交织矢量 [英] Interweaving vectors

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

问题描述

我想在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天全站免登陆