필터 지우기
필터 지우기

How to change all elements of a vector V to achieve the condition sum(V) equal with a upper and lower boundaries?

조회 수: 1 (최근 30일)
I have a vector V, for example, V=[0.1 0.002 0.5 0.2 0.1 0.003 0.4].
The boundaries of all elements of the matrix V should always be between 0.01 and 0.8.
I want to create a function to change the elements of the vector V where the sum of V becomes equal to one.
I am looking to create a function in Matlab V= Editor(V, lp, up );
  • where lp: is the lower boundary, in my example 0.01
  • and up: is the upper boundary, in my example 0.8
  댓글 수: 4
Rahim Rahim
Rahim Rahim 2022년 12월 29일
I want to change all the variable of the vector !
@Rik I know that, but I want to change the variable of V and the V after the modification shold alwaus respects the up and lower bounderies

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2022년 12월 29일
편집: DGM 2022년 12월 29일
Well with requirements like that, I guess this is fair game.
% input
V = [0.1 0.002 0.5 0.2 0.1 0.003 0.4];
% parameters
limits = [0.01 0.8];
outsum = 1;
% constraints cannot be met generally
if outsum/numel(V) < limits(1)
error('too many elements')
elseif outsum/numel(V) > limits(2)
error('too few elements')
end
% i'm going to be super lazy
sumv = sum(V);
while abs(sumv-outsum) > 1E-9 % or some arbitrary tolerance
scale = outsum/sumv;
V = min(max(V*scale,limits(1)),limits(2));
sumv = sum(V);
end
V
V = 1×7
0.0754 0.0100 0.3769 0.1508 0.0754 0.0100 0.3015
  댓글 수: 4
Rahim Rahim
Rahim Rahim 2022년 12월 29일
@DGM Thank you so much
But I want everytime the function generat a different matrix ... ( many solution )
and thank you so much
DGM
DGM 2022년 12월 29일
Nothing about the question suggested that the output was random. If the output is random, what's the purpose of V? Does the output depend on V? If so, how?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt J
Matt J 2022년 12월 29일
편집: Matt J 2022년 12월 29일
V=[0.1 0.002 0.5 0.2 0.1 0.003 0.4];
lp=0.01;
up=0.8;
n=numel(V);
V(:)=(1-n*lp)/n+lp
V = 1×7
0.1429 0.1429 0.1429 0.1429 0.1429 0.1429 0.1429
sum(V)
ans = 1.0000
V>=lp
ans = 1×7 logical array
1 1 1 1 1 1 1
V<=up
ans = 1×7 logical array
1 1 1 1 1 1 1

Walter Roberson
Walter Roberson 2022년 12월 29일
if the requirement is that you apply a linear transform to the elements of V producing a new vector W such that sum(W) == 1 and the elements respect the upper and lower bounds, then the problem cannot generally be solved.
A simple proof is that V might consist of more than 100 elements. With the minimum being 0.01 then the sum would have to be more than 100*0.01 == 1
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 12월 29일
That said
syms a
w = (v-0.01)*a + 0.01;
sola = solve(sum(w)==1,a);
w = simplify(w, a, sola)
sola can be expressed analytically so it can be computed without the symbolic toolbox
Walter Roberson
Walter Roberson 2022년 12월 29일
each entry w(k) is a*(v(k)-0.01)+0.01 which is a*v(k) - a*0.01 + 0.01
Let N = length(v). Then sum(w) is a*sum(v) - N*a*0.01 + N*0.01 = a*(sum(v) - N*0.01) + N*0.01. The unknown is a and the sum is 1 so a = (1-N*0.01) / (sum(v) - N*0.01)
Note that if the original values in v could be negative or less than 0.01 then the denominator could be 0 which would be a problem. If the denominator is negative because entries in v are less than 0.01 then I do not promise at the moment that the transformed values are within the required limits.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by