创建一个矢量

时间:2012-03-07 22:36:02

标签: matlab vector indexing vectorization

输出向量应如下所示:

a=[3 3 3 4 4 4 4 5 5 5 5 5]

我拥有的是:

pe=[1 5 9] and ne=[4 8 12] and co=[3 4 5]

pe描述了每个条目的起始索引和结束索引,以及该条目的值

我想在没有循环的情况下这样做。 使用Loop它应该如下所示:

  for i=1:3
     a(pe(i):ne(i))=co(i)
   end

1 个答案:

答案 0 :(得分:2)

执行此操作的一种方法是首先使用co

创建一个指数数组cumsum
idxList = zeros(1,max(ne)); %# create an array with zeros
idxList(pe) = 1;            %# mark the start of a new index
idxList = cumsum(idxList);  %# now, idxList has 1's where we should
                            %# place the first element of co, etc
out = co(idxList);          %# and we're done. 
相关问题