Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Rounding Digits
Replies: 1   Last Post: May 4, 2014 5:44 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Derek Goring

Posts: 3,919
Registered: 12/7/04
Re: Rounding Digits
Posted: May 4, 2014 5:44 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Monday, May 5, 2014 9:22:08 AM UTC+12, Kris wrote:
> Im trying to create a program that will round any number to any specific digit. I have an array of numbers in column A in an excel document, then the specific digit I want that number rounded to in column B. So, I will use xlsread to gather that information from excel to start. Then I will use xlswrite to send that information to excel in column C (answer). I have a mac so I can see if my code is working or not. I'd appreciate any help.
>
>
>
>
>
> matrix = xlsread('Round.xlsx','A');
>
> numbers = matrix(:,1);
>
> digits = matrix(:,2);
>
>
>
> normNums = numbers./digits;
>
> n = length(normNums);
>
> signNums = ones(n, 1);
>
> roundedNormNums = zeros(n, 1);
>
> roundedNums = zeros(n, 1);
>
>
>
> signNums(normNums < 0) = -1;
>
> normNums(normNums < 0) = -normNums(normNums < 0);
>
>
>
>
>
> for i = 1:n
>
> while (normNums(i)-roundedNormNums(i)) > 0.5
>
> roundedNormNums(i) = roundedNormNums(i)+1;
>
> end
>
> end
>
>
>
> %% Calculates the Output
>
>
>
> roundedNums = signNums.*roundedNormNums.*digits;
>
> column_C = xlswrite('Round.xlsx', roundedNums , 'C', sprintf('C1:C%d', n));


Why not simply:
normNums = numbers./digits;
roundedNums=round(normNums).*digits;


Date Subject Author
5/4/14
Read Rounding Digits
Kris
5/4/14
Read Re: Rounding Digits
Derek Goring

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.