필터 지우기
필터 지우기

How to round decimals to other decimal values already defined

조회 수: 1 (최근 30일)
Pilar Jiménez
Pilar Jiménez 2017년 11월 29일
댓글: Stephen23 2017년 12월 1일
Hello, I would like to request your support to know how to round certain decimal values to others closer. For example, if I have [0.359 0.679 0.890 0.5653] I would like to know how I can round up these values to only take the following [0.2 0.4 0.6 0.8] depending on the value in which they are. So for example 0.359 would be rounded to 0.4, 0.679 to 0.8, etc. The criterion in the example is: less than 0.65 is the value of the nearest smaller number that is 0.6 and greater than or equal to 0.65 is the value of the next higher number that would be 0.8

채택된 답변

KVM
KVM 2017년 11월 29일
This is a way of doing it! :
a = [0.359 0.679 0.890 0.5653];
b = [0.2 0.4 0.6 0.8];
newA =0;
for iLoop = 1:length(a)
c = b-a(iLoop);
d = c( c>=0 );%Keep only Positive Value
[e index] = min(d);
if ~isempty(d);
newA(iLoop) = e+a(iLoop);
else
newA(iLoop) =0; % Default Value
end
end
newA %Display Result
Display: newA =
0.4000 0.8000 0 0.6000
  댓글 수: 1
Pilar Jiménez
Pilar Jiménez 2017년 11월 30일
Thank you very much, it is a way of doing it that I would have never considered it. It will definitely help me a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by