增量与利用Matlab约束数组值? [英] Increment values in an array with constraints using Matlab?

查看:134
本文介绍了增量与利用Matlab约束数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情景:结果
如果我有4个负载(A1 A2 A3 A4)

Scenario :
If I have an array with 4 loads (a1 a2 a3 a4)

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

我想试试,增加阵列中的所有值3.结果
注:数组 A 是不固定的,并且可以从任何值 0:3

I would like to try and increase all values in the array to 3.
Note : the array a is not fixed, and can have any value from 0:3

约束:


  1. 还有就是不能违反优先排列

  2. 总增量的数量限制为3

考虑:

优先级排列 V = [1 3 2 1] - (1为最高优先级,3是最低优先级)结果
注:数组 v 是不固定的,并且可以从任何值 0:3

Priority array v=[1 3 2 1] -- (1 is highest priority, and 3 is lowest priority).
Note : the array v is not fixed, and can have any value from 0:3

使用这个优先级排列:

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

执行情况,对我的审判伪code:

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]达到,那么 A 从左至右优先(我还没有找到一个更好的方式来做到这一点)

Note : if a priority value = [1 1 1 1] is reached, then a is prioritised from left to right (I haven't found a better way to do this)

我希望这是有道理的,而且我的伪code显示我想要实现。问我,如果事情是不明确的。

I hope this makes sense, and that my pseudo code shows what I'm trying to implement. Ask me if something is not clear.

推荐答案

您可以做这样的事情。

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

的关键步骤是优先级排列的排序,以及一个由相同的索引排序。那么你可以通过一个循环,是相信你具有最高优先级开始。

The key step is the sorting of the priority array, and the sorting of a by the same indices. Then you can just loop through a, being confident that you're beginning with the highest priority.

如果您需要将相同的顺序在输出的输入,那么你就可以颠倒通过执行排序操作

If you require the same order as the input at output, then you can invert the sorting operation by doing

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

该阵列五世不是摆在首位修改,所以你不需要反转的阵列上的排序。

The array v was not modified in the first place, so you don't need to invert the sort on that array.

这篇关于增量与利用Matlab约束数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆