2013-03-21 38 views
1

場景:
如果我有4個負載(A1 A2 A3 A4)使用Matlab在具有約束的數組中增加值?

a=[a1 a2 a3 a4] (locations of these loads must be fixed) 
a=[1 2 3 3] 

一個數組,我想嘗試和增加陣列中的所有值3
注:陣列a是不固定的,並且可以具有從0:3

約束的任何值:

  1. 有不能被侵犯
  2. 總增量的數量限制爲3

給予優先級陣列:

優先陣列v=[1 3 2 1] - (1是最高優先級, 3是最低優先級)。
注:數組v是不固定的,並且可以具有0:3

任何值使用這個優先級排列:

a(1,1)=highest priority 
a(1,4)=2nd highest priority 
a(1,3)=3rd priority 
a(1,2)=lowest priority 

實施,對我的審判在僞代碼:

a=[1 2 3 3] 
v=[1 3 2 1] 
count=3 

Check highest priority : a(1,1) 
increment by 1 
decrement count by 1 
count = 2 
still less than 3 ? if yes, then increment again until a(1,1)<= 3 AND count >=0 
Change highest priority to 5 (so that min(v) will not pick it up) 

ans : a=[3 2 3 3] ; v=[5 2 3 3] ; count = 1 

Check highest priority : a(1,3) 
value >= 3 
Change highest priority to 5 (so that min(v) will not pick it up) 
skip 

ans : a=[3 2 3 3] ; v=[5 2 5 3] ; count = 1 

Check highest priority : a(1,4) 
value >=3 
Change highest priority to 5 (so that min(v) will not pick it up) 
skip 

ans : a=[3 2 3 3] ; v=[5 2 5 5] ; count = 1 

Check highest priority : a(1,2) 
increment by 1 
decrement count by 1 
count = 0 
still less than 3 ? if yes, then increment again until a(1,1)<= 3 AND count >=0 
Change highest priority to 5 (so that min(v) will not pick it up) 

ans = [a1 a2 a3 a4] = [3 3 3 3] 

注意:如果達到優先值= [1 1 1 1],則t母雞a從左至右優先(我還沒有找到更好的辦法來做到這一點)

我希望這是有道理的,而且我的僞代碼顯示了什麼,我想實現。問我是否有不清楚的地方。

回答

1

你可以做這樣的事情

a = [1 2 3 3]; 
v = [1 3 2 1]; 

% Sort a in the order of priority v 
[vSrt, indSrt] = sort(v); 
a = a(indSrt); 

nIncsRemaining = 3; % Total no. of increments allowed 
target = 3; % Target value for each value in a 

for curInd = 1:length(a) 
    % Difference from target 
    aDiff = target - a(curInd); 
    % Do we need to increment this value of a? 
    if aDiff > 0 
     % Increment by a maximum of nIncsRemaining 
     aDelta = min(aDiff, nIncsRemaining); 
     % Increment a and decrement no. of increments remaining by the 
     % same amount 
     a(curInd) = a(curInd) + aDelta; 
     nIncsRemaining = nIncsRemaining - aDelta; 
    end 

    % Have we done as much as we're allowed? 
    if nIncsRemaining == 0, break; end 
end 

的關鍵步驟是優先數組的排序,以及一個由同一指數的排序。然後你可以循環一遍,並確信你的優先級最高。

如果您需要相同的順序在輸出的輸入,那麼你可以通過做

[~, indReSrt] = sort(indSrt); 
a = a(indReSrt); 

數組v的沒有在第一時間修改反轉排序操作,所以你不需要顛倒該陣列上的排序。

+0

但不要陣列後留在相同的位置?我會更新問題以澄清我的意思。因爲答案必須是'[a1 a2 a3 a4]',所以不能混洗。 – NLed 2013-03-21 22:07:36

+0

哦,你能解釋一下代碼,想知道發生了什麼。 – NLed 2013-03-21 22:11:44

+0

編輯的答案包括解釋評論和要求的訂單;它現在有意義嗎? – wakjah 2013-03-21 22:19:47

1

另一個版本:

a = [1 2 3 3]; 
v = [1 3 2 1]; 
count = 3; 
target = 3; 

排序av在優先順序

[vSorted, order] = sort(v); 
aSorted = a(order); 
將導致

查找位置count等於0

pos = find(cumsum(target - aSorted) >= count); 

更新所有的值,直到但不包括pos,遞減count相應

count = count - sum(3 - aSorted(1:pos - 1)); 
vSorted(1:pos - 1) = 5; 
aSorted(1:pos - 1) = target; 

更新在pos S的值

aSorted(pos) = aSorted(pos) + count; 
count = 0; 
if aSorted(pos) == target 
    vSorted(pos) = 5; 
end 

恢復排序順序

[~, invOrder] = sort(order); 
a = aSorted(invOrder); 
v = vSorted(invOrder); 

如果v僅用於確定優先級沒有必要更新它。 如果count可能在a的所有值達到target之後仍然不爲零,則需要對該情況進行一些額外處理,因爲這將導致pos = find(...);返回空數組。

1

這就是我想出了:

a = [1 2 3 3]; 
v = [1 3 2 1]; 

% Get priority vector - this converts v into the indices of a that are most important in descending order. This can also be preallocated for speed or stored in place if v is not important; 
priority_vec = []; 
for i = 0:3 
    % Get indices 
    priority_vec = horzcat(priority_vec,find(v == i)); 
end 

% Loop over priority_vec 
count = 3; % count is the number of additions you can make 
for i = 1:4 % Loop over the indices of priority vec 
    priority_ind = priority_vec(i); % This is the current index of most importance 
    while(a(priority_ind) < 3 && count ~= 0) % Continue to add one while count is greater than 0 and the value at the priority index is less than three    
     a(priority_ind) = a(priority_ind) + 1; 
     count = count - 1; 
    end  
end 
+0

你能提供更多關於你的代碼的信息嗎?想了解該循環中發生了什麼 – NLed 2013-03-21 22:49:05

+0

@NLed在一些評論中添加了。 – Justin 2013-03-21 22:53:46

+0

非常感謝你! – NLed 2013-03-21 23:05:51