2012-03-07 114 views
2

輸出載體應該是這樣的:創建矢量

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描述的起始索引和NE從每個條目結束索引和共同的價值此條目

我想這樣做沒有循環。 帶環也應該是這樣的:

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

的用於i循環應該是PE(I) ...等等,對嗎?另外,你想如何處理pe = [1 4 9]和ne = [6 8 12]和co = [3 4 5]的情況?使用最新值? – 2012-03-07 22:58:36

+0

yes ..my fault sorry .. it has to be a(pe(i):ne(i))= co(i)。在另一種情況下,我只是想用pe,ne和co創建矢量a (1)= 3的第一個條目,並且pe的第一個條目是co(1)的最後一個條目)= 3等等。 – 2012-03-07 23:27:56

回答

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. 
+0

謝謝!基本上它很簡單,但它並沒有出現在我的腦海裏! – 2012-03-07 23:43:14

+0

@HakanKiyar:不客氣。如果您發現答案有幫助,請考慮接受它。 – Jonas 2012-03-08 04:56:29