場景:
如果我有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
約束的任何值:
- 有不能被侵犯
- 總增量的數量限制爲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
從左至右優先(我還沒有找到更好的辦法來做到這一點)
我希望這是有道理的,而且我的僞代碼顯示了什麼,我想實現。問我是否有不清楚的地方。
但不要陣列後留在相同的位置?我會更新問題以澄清我的意思。因爲答案必須是'[a1 a2 a3 a4]',所以不能混洗。 – NLed 2013-03-21 22:07:36
哦,你能解釋一下代碼,想知道發生了什麼。 – NLed 2013-03-21 22:11:44
編輯的答案包括解釋評論和要求的訂單;它現在有意義嗎? – wakjah 2013-03-21 22:19:47