Moving between the condition
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2017-12-14
编辑:Jan
2017-12-14
@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?
回答(2 个)
Image Analyst
2017-12-14
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))
0 个评论
Jan
2017-12-14
编辑:Jan
2017-12-16
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));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!