# Round values to specific number

55 Ansichten (letzte 30 Tage)
Amit Ifrach am 7 Feb. 2023
Beantwortet: Amit Ifrach am 9 Feb. 2023
לק"י
Hi,
I try to discretize vector A by vector edges. for example:
edges = [0:10^5:max(A)];
dsc = discretize(A, edges);
The problem is that the edges vectorcreated rounds down the value to 290000:
[0:10^5:max(aca2_100123pacd3)]
ans =
Columns 1 through 12
.....
Columns 25 through 30
2400000 2500000 2600000 2700000 2800000 2900000
I need the range of edges to be bigger than the max(A) so the dsc of max(A) will give a number and not a NaN output.
Any ideas how could it be done easily withouth writing about 3-5 lines?
I saw the round to 100 at the matlab round page, but want something that will suite not only 100, but any desiried number:
Round the number 863178137 to the nearest multiple of 100.
round(863178137,-2)
ans = 863178100
Thanks!
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Voss am 7 Feb. 2023
"want something that will suit not only 100, but any desiried number"
You can do round(val./d).*d where val is the original number(s) and d is the number(s) you want the result(s) to be a multiple of.
Examples:
% round 1100 to the nearest multiple of 3:
val = 1100;
d = 3;
result = round(val./d).*d
result = 1101
% round 102 to the nearest multiple of 7:
val = 102;
d = 7;
result = round(val./d).*d
result = 105
% round 10 to the nearest multiple of 3.7:
val = 10;
d = 3.7;
result = round(val./d).*d
result = 11.1000
% round 10:10:100 to the nearest multiple of 9:
val = 10:10:100;
d = 9;
result = round(val./d).*d
result = 1×10
9 18 27 36 54 63 72 81 90 99
% round 11 to the nearest multiple of 7, round 12 to the nearest multiple of 5, round 13 to the nearest multiple of 6:
val = [11 12 13];
d = [7 5 6];
result = round(val./d).*d
result = 1×3
14 10 12
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (2)

Jan am 7 Feb. 2023
The colon operator creates a vector from the 1st argument in steps of the 2nd argument until the 3rd argument, but it does not include the last value necessarily. If you want it, include it explicitly:
maxA = max(A);
edges = [0:1e5:maxA, maxA];
But is maxA is divisable by the stepsize, it appears twice in the vector. If you want to avoid this, write a simple function to solve this:
function v = ColonInclude(a,b,c)
v = a:b:c;
if v(end) < c
v(end + 1) = c;
end
end
Matlab is a programming language. Why do you want to use it "withouth writing about 3-5 lines"?
##### 2 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Steven Lord am 7 Feb. 2023
Another way to do this is to add (b-1) to the upper limit. If c is exactly a multiple of b away from a that's not far enough to get c+b added to the vector but if it's any larger you get the extra point.
I'm assuming everything here is an integer value. Otherwise you might need to get tricky and add b-someVerySmallNumber to the upper limit.
a = 0;
b = 10;
c = 121;
v = a:b:(c+b-1)
v = 1×14
0 10 20 30 40 50 60 70 80 90 100 110 120 130
c = 120;
v = a:b:(c+b-1)
v = 1×13
0 10 20 30 40 50 60 70 80 90 100 110 120
c = 129;
v = a:b:(c+b-1)
v = 1×14
0 10 20 30 40 50 60 70 80 90 100 110 120 130

Melden Sie sich an, um zu kommentieren.

Amit Ifrach am 9 Feb. 2023
Thank guys, you all helped me very much!
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu NaNs finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!