输出向量应如下所示:
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 描述每个条目的起始索引和结束索引,co 描述该条目的值
我想在没有循环的情况下执行此操作。 使用循环它应该看起来像这样:
for i=1:3
a(pe(i):ne(i))=co(i)
end
最佳答案
实现此目的的一种方法是首先使用 cumsum
创建一个索引数组到 co
中
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.
关于matlab - 创建向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9610207/