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 Kommentare

Jan
Jan am 14 Dez. 2017
Bearbeitet: Jan am 14 Dez. 2017
I cannot follow your description. Posting code, which does not do what you want, does not clear this also. Can you provide a short example of the inputs and the wanted outputs?
Vennila Gangatharan
Vennila Gangatharan am 14 Dez. 2017
Bearbeitet: Vennila Gangatharan am 14 Dez. 2017
Input array=>68,23,78,45,22,40,80
Output array=> 62,22,70,43,21,34,69
This output should be generated randomly within intervals in which the value lies such as 1 to 20, 21 to 40, 41 to 60 and 61 to 80 and also less than it.
Stephen23
Stephen23 am 14 Dez. 2017
Bearbeitet: Stephen23 am 14 Dez. 2017
@Vennila Gangatharan: what relationship do those ranges have to the input values?
Jan
Jan am 14 Dez. 2017
Bearbeitet: Jan am 14 Dez. 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?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 14 Dez. 2017

1 Stimme

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 am 14 Dez. 2017
Bearbeitet: Jan am 16 Dez. 2017

0 Stimmen

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));

Kategorien

Mehr zu Random Number Generation finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 14 Dez. 2017

Bearbeitet:

Jan
am 16 Dez. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by