Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8204955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:03:43+00:00 2026-06-07T08:03:43+00:00

In GNU Octave version 3.4.3 I want to round a matrix to 2 units

  • 0

In GNU Octave version 3.4.3 I want to round a matrix to 2 units precision on the contents of a matrix like this.

mymatrix=[1.1234567, 2.12345; 3.1234567891, 4.1234];
disp(mymatrix);

This prints:

1.1235   2.1235
3.1235   4.1234

As you can see, the disp forces the precision to ‘5’, I want the units precision to be 2. How do I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:03:44+00:00Added an answer on June 7, 2026 at 8:03 am

    How to round off elements in a matrix in Octave:

    There are many different ways to round a matrix and round a number in octave.

    Option 1, use of sprintf format feature

    mymatrix=[100.1234567, 2.12345; 3.1234567891, 4.1234];
    rows = rows(mymatrix);
    cols = columns(mymatrix);
    for i = 1:rows
      for j = 1:cols
        sprintf("%5.2f", mymatrix(j,i))
      endfor
    endfor
    

    Output, note the "%5.2f" token. The ‘f’ means expect a float, the 5 means occupy 5 spaces. The 2 means 2 units precision after the decimal point.

    ans = 100.12
    ans =   3.12
    ans =   2.12
    ans =   4.12
    

    Option 2, round to significant digits using eval and mat2str

    mymatrix2=[100.1234567, 2.12345; 3.1234567891, 4.1234];
    j = mat2str(mymatrix2, 3);
    mymatrix2=eval(j)
    

    Output, matrix rounded to 3 significant digits, notice 100.123 rounded to 100 while the 2.12345 was rounded to 2.12

    mymatrix2 = 100.0000     2.1200
                  3.1200     4.1200
    

    Option 3, use the round function

    The round function does not have a precision parameter in Octave. However you can hack around it by multiplying each item in the matrix by 100, rounding it to the nearest int, then dividing each item by 100:

    mymatrix=[100.1234567, 2.12345; 3.1234567891, 4.1234];
    round(mymatrix .* 100) ./ 100
    

    Output, round occurs correctly:

    ans = 100.1200     2.1200
            3.1200     4.1200
    

    Option 4, specify a output_precision(num)

    You noticed that option 3 above kept the trailing zeros, which may be undesirable, so you can tell them to go away by setting output_precision:

    mymatrix=[100.1234567, 2.12345; 3.1234567891, 4.1234];
    disp(mymatrix);
    output_precision(3)
    disp(mymatrix)
    

    Output:

    100.1235     2.1235
      3.1235     4.1234
    
    100.123     2.123
      3.123     4.123
    

    Octave has some odd behavior when trying to do rounding because octave tries hard to uniformly apply a uniform rounding to all items in a matrix. So if you have multiple columns with wildly different values, octave sees a tiny value and says: "I should convert that to an exponential like 1.0e-04, and so the same exponential is applied to the entire data structure in the matrix.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Matlab or GNU Octave I'd like to do something like this: x=struct('a',1,'b',2,'c',[1 2;3
I'm using gnu as for a simple boot. It's something like this: .text .org
Using GNU bash (version 4.0.35(1)-release (x86_64-suse-linux-gnu), I would like to negate a test with
GNU bash, version 4.1.5(1)-release-(i486-pc-linux-gnu) Ubuntu Linux 10.04 This prints var as 1234 and does
In GNU Octave version 3.4.3, I am having trouble applying a custom function to
GNU bash, version 4.2.24: $> printf %.0f, %.0f\n 48.5 49.5 48, 50 Ruby 1.8.7
The GNU/Linux version of cp has a nice --update flag: -u, --update copy only
(this is similar to GNU make: Execute target but take dependency from file but
I recently downloaded the latest stable release of Gnu Octave, and was attempting to
I'm currently working on writing a version of the MATLAB RegionProps function for GNU

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.