2016-12-28 87 views
0

我需要用幾個較小陣列填充大陣列。小數據從具體索引開始填充大數組。例如:用較小陣列的數據填充大陣列

a = [0 0 0 0 0 0 0 0 0 0]; 
b = [1 2 3 ]; 

r = [0 0 0 1 2 3 0 0 0 0]; 

在此外,它應該做的事,將允許交叉數據,總結和不覆蓋,這樣的方式:

a = [0 0 0 1 2 3 0 0 0 0]; 
c = [3 2 1]; 

r = [0 3 2 2 2 3 0 0 0 0]; 

感謝。

回答

0

這其實很簡單:

function q41370447 
    ind = [4, 2]; 
    a = zeros(1,10); 
    b = 1:3; 
    c = 3:-1:1; 

    a = addFromIndex(a,b,ind(1)); 
    % [0,0,0,1,2,3,0,0,0,0] 
    a = addFromIndex(a,c,ind(2)); 
    % [0,3,2,2,2,3,0,0,0,0] 
end 

function largeVec = addFromIndex(largeVec,smallVec,startIndex) 
    n = numel(smallVec); 
    largeVec(startIndex:startIndex+n-1) = largeVec(startIndex:startIndex+n-1) + smallVec; 
end 
0
a=[0 0 0 1 2 3 0 0 0 0]; 
c=[3 2 1]; 

your_concrete_index = 2; 
cc = zeros(1,length(a)); %same length, filled with zeros 
cc(your_concrete_index:your_concrete_index+length(c)-1) = c;%from index, whole c 
a_result = a+cc; % just sum..