I want to double numbers that are less than the given parameter. What do I need to change
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
function dataSamples = AdjustMinValue(numberSamples, userSamples, minValue)
% numberSamples: Number of data samples in array dataSamples
% userSamples : User defined array
% minValue : Minimum value of any element in array
% Write a for loop that iterates from 1 to numberSamples to double any element's
% value in dataSamples that is less than minValue
for i = 1:numberSamples
if userSamples(i) > minValue
dataSamples = userSamples * 2
end
end
end
This code doubles all user samples. It should only double numbers less minValue.
Ex.) if minValue = 10 then dataSamples [2,12,9,20] becomes [4,12,18,20]
0 comentarios
Respuestas (1)
Giuseppe Inghilterra
el 22 de Feb. de 2020
Editada: Giuseppe Inghilterra
el 22 de Feb. de 2020
Your function will be:
function dataSamples = AdjustMinValue(numberSamples, userSamples, minValue)
% numberSamples: Number of data samples in array dataSamples
% userSamples : User defined array
% minValue : Minimum value of any element in array
% Write a for loop that iterates from 1 to numberSamples to double any element's
% value in dataSamples that is less than minValue
for i = 1:numberSamples
if userSamples(i) > minValue
dataSamples(i) = userSamples(i);
else
dataSamples(i) = userSamples(i)*2;
end
end
end
However, you don't need a for loop, you can just run following one-line code based on your last example:
dataSamples(dataSamples<minValue) = dataSamples(dataSamples<minValue)*2;
If we applied to your function, it becomes:
function dataSamples = AdjustMinValue(numberSamples, userSamples, minValue)
dataSamples = userSamples;
dataSamples(dataSamples<minValue) = dataSamples(dataSamples<minValue)*2;
end
1 comentario
Jackson
el 9 de Dic. de 2022
for i = 1:numberSamples
if userSamples(i) >= minValue
dataSamples(i) = userSamples(i);
else
dataSamples(i) = userSamples(i)*2;
end
end
end
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!