在MATLAB中的向量中的非零元素之间插入零个变量 [英] Inserting variable number of zeros between non-zero elements in a vector in MATLAB

查看:512
本文介绍了在MATLAB中的向量中的非零元素之间插入零个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的向量:

I have a vector like:

a = [1,2,3,4,5,6...,n]

我想获得一个像这样的新向量:

and I would like to obtain a new vector like this:

a_new = [1,0,0,2,0,0,3,0,0,4,0,0,5,0,0,6,...,0,0,n]

在非零元素之间插入固定数量的零(在上面的示例中为2).如果选择zero_p=3,则新矢量将为:

where a fixed number of zeros (2 in the above example) are inserted between the non-zero elements. If I choose zero_p=3, the new vector would be:

a_new = [1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0,5,0,0,0,6,...,0,0,0,n]

我该怎么做?

推荐答案

尝试一下:

zero_p=3;
a_new=zeros(1, (zero_p+1)*length(a)-zero_p);
a_new(1:(zero_p+1):end)=a;

(未经测试,但有望工作.)

(Untested, but should hopefully work.)

这篇关于在MATLAB中的向量中的非零元素之间插入零个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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