Moving between the condition

4 visualizaciones (últimos 30 días)
Vennila Gangatharan
Vennila Gangatharan el 14 de Dic. de 2017
Editada: Jan el 16 de Dic. de 2017
I am trying to replace an array values less than the orginal by generating random values between some range among them. I tried but the values are replaced by larger values than the orginal in some cases. So i have to use another condition to check this case and have to redo the random generation if the value goes greater than its original. I don't know how to jump again to the condition.Here is my code
for i= 1 :x
v = value(i);
if(v>=min_value) && (v<=p10)
rval = min_value + rand * (p10 - min_value);
*if (rval<v)
new(i,cnt+1) = rval;
else*
elseif (v>p10) && (v<=p20)
new(i,cnt+1) = p10+ rand * (p20 - p10);
*if (rval<v)
new(i,cnt+1) = rval;
else*
end
end
  4 comentarios
Stephen23
Stephen23 el 14 de Dic. de 2017
Editada: Stephen23 el 14 de Dic. de 2017
@Vennila Gangatharan: what relationship do those ranges have to the input values?
Jan
Jan el 14 de Dic. de 2017
Editada: Jan el 14 de Dic. de 2017
@Vennila Gangatharan: Although the readers might be able to guess a little bit, it is more reliable, if you explain carefully, what your code does and should do. What is "x", "value", "min_value", "p10", "p20"? Is "input array" the same as "value" and "output array" the same as "new"? You code looks like you want floating point values, but the data look like integer values. The less the reader has to guess, the more likely is that you get a matching answer.
If the input is 22, what is the allowed range for the output? Does the allowed range depend on the current input value only, or do other input or output values matter also?

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 14 de Dic. de 2017
Try this for integers:
inputArray = [68,23,78,45,22,40,80]
minValue = min(inputArray)
maxValue = max(inputArray)
outputArray = randi([minValue, maxValue], 1, length(inputArray))
Try this for floating point:
inputArray = [68,23,78,45,22,40,80]
minValue = min(inputArray)
maxValue = max(inputArray)
outputArray = minValue + (maxValue - minValue) * rand(1, length(inputArray))

Jan
Jan el 14 de Dic. de 2017
Editada: Jan el 16 de Dic. de 2017
A bold guess: You want to choose a number between the next smaller multiple of 20 and the value. If so:
In = [68,23,78,45,22,40,80];
Out = zeros(size(In));
for k = 1:numel(In)
v = In(k);
low = v - mod(v, 20) + 1; % Or: floor(v / 20) * 20 + 1;
high = low + 19;
Out(k) = randi([low, high]);
% or: = low + 20 * rand; % Or 19 ?!
end
If this solves your problem, it can be vectorized easily for the floating point method:
low = In - mod(In, 20) + 1;
Out(k) = low + 20 .* rand(size(In));

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by