Round values to specific number (2024)

77 views (last 30 days)

Show older comments

Amit Ifrach on 7 Feb 2023

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number

Answered: Amit Ifrach on 9 Feb 2023

Accepted Answer: Voss

Open in MATLAB Online

לק"י

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 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Voss on 7 Feb 2023

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#answer_1165675

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#answer_1165675

Open in MATLAB Online

"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 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

More Answers (2)

Jan on 7 Feb 2023

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#answer_1165650

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#answer_1165650

Open in MATLAB Online

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 Comments

Show NoneHide None

Bjorn Gustavsson on 7 Feb 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#comment_2603440

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#comment_2603440

Open in MATLAB Online

Should be possible to golf this a bit:

maxA = max(A);

edges = unique([0:1e5:maxA, maxA]);

Steven Lord on 7 Feb 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#comment_2603470

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#comment_2603470

Open in MATLAB Online

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

Sign in to comment.

Amit Ifrach on 9 Feb 2023

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#answer_1167405

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1908065-round-values-to-specific-number#answer_1167405

Thank guys, you all helped me very much!

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsLine Plots

Find more on Line Plots in Help Center and File Exchange

Tags

  • round values

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Round values to specific number (7)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Round values to specific number (2024)
Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6058

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.