Arrays 使用Matlab在具有约束的数组中增加值?
场景:Arrays 使用Matlab在具有约束的数组中增加值?,arrays,matlab,compare,Arrays,Matlab,Compare,场景: 如果我有一个具有4个加载的阵列(a1 a2 a3 a4) 我想尝试将数组中的所有值增加到3。 注意:数组a不是固定的,可以有0:3 约束条件: 存在不能违反的优先级数组 总增量的数量限制为3 给定: 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 in
如果我有一个具有4个加载的阵列(a1 a2 a3 a4) 我想尝试将数组中的所有值增加到3。
注意:数组
a
不是固定的,可以有0:3
约束条件:
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]
优先级数组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]
a=[1233]
v=[1 3 2 1]
计数=3
检查最高优先级:a(1,1)
增加1
递减计数1
计数=2
还不到3个?如果是,则再次递增,直到a(1,1)=0
将最高优先级更改为5(以便最小(v)不会拾取它)
ans:a=[3 2 3];v=[5 2 3];计数=1
检查最高优先级:a(1,3)
值>=3
将最高优先级更改为5(以便最小(v)不会拾取它)
跳过
ans:a=[3 2 3];v=[5 2 5 3];计数=1
检查最高优先级:a(1,4)
值>=3
将最高优先级更改为5(以便最小(v)不会拾取它)
跳过
ans:a=[3 2 3];v=[5 2 5];计数=1
检查最高优先级:a(1,2)
增加1
递减计数1
计数=0
还不到3个?如果是,则再次递增,直到a(1,1)=0
将最高优先级更改为5(以便最小(v)不会拾取它)
ans=[a1 a2 a3 a4]=[3]
注意:如果达到优先级值=[1],则a
从左到右排列优先级(我还没有找到更好的方法)
我希望这是有意义的,并且我的伪代码显示了我试图实现的内容。问我是否有什么不清楚的地方。你可以这样做
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一开始没有修改,因此不需要反转该数组上的排序。另一个版本:
a = [1 2 3 3];
v = [1 3 2 1];
count = 3;
target = 3;
按优先级顺序排序a
和v
[vSorted, order] = sort(v);
aSorted = a(order);
查找将导致计数等于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
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
仅用于确定优先级,则无需对其进行更新。
如果
a
的所有值达到target
后,count
可能仍然非零,则需要对该情况进行一些额外处理,因为这将导致pos=find(…)
返回空数组。以下是我的想法:
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
a=[1233];
v=[1 3 2 1];
%Get priority vector-这将v转换为a的索引,这些索引按降序排列最重要。如果v不重要,也可以预先分配速度或存储在适当位置;
优先级向量=[];
对于i=0:3
%获取索引
优先级向量=horzcat(优先级向量,find(v==i));
结束
%循环优先权
计数=3;%count是您可以添加的数量
对于i=1:4%的优先级向量索引循环
优先级=优先级向量(i);%这是目前最重要的指标
而(a(priority_ind)<3&&count~=0)%count大于0且优先级索引处的值小于3时,继续添加一
a(优先级标识)=a(优先级标识)+1;
计数=计数-1;
结束
结束
但是阵列在之后是否保持相同的位置?我将更新问题以澄清我的意思。由于答案必须是[a1 a2 a3 a4]
,它们不能被洗牌。哦,你能解释一下代码吗?我想知道发生了什么。答案经过编辑,包括解释注释和要求的顺序;现在有意义了吗?你确定这是对的吗?如果将nincsraining
更改为2,则输出为[3 2]
。如果我正确理解了您的问题,您希望a
的第二个索引具有最低优先级,因此输出应该是[3 2 3]
@wakjah您能检查一下吗?更改nincsreping
的值不会给出正确的解决方案。请提供有关代码的更多信息?想知道这里面发生了什么吗loop@NLed添加了一些评论。