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
CodePudding user response:
Try making a variable(maybe call it sumV) to store the sum of everything in V and another variable which stores 1/sumV. This variable would be what you use to change the elements of the vector. You could use lp and up to check that the values in V didn't go over or under your bounds with a for loop that went over all the elements in v.
Some psuedo code:
for i = 1:length(V)
if statement checking if V(i) was less than the lower boundary 'lp'
update any values less than 'lp' to 'lp'
if statement checking if V(i) was greater than the upper boundary 'up'
update any values greater than 'up' to 'up'
end of for loop
now sum all elements in V to a variable e.g. sumV
divide 1 by sumV to get your scale factor which is used to modify the contents of V
loop over all elements in V and times them all by the scale factor*
Hope that answers your question :)