Truncating a Number to 5 Significant Digits in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter asset101
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on creating a MATLAB function to truncate an integer to five significant digits. The key logic involves using the base 10 logarithm function, log10, to determine the number of digits in the input number. If the number exceeds 99999, it is truncated accordingly, while numbers less than or equal to 99999 remain unchanged. The formula digits = floor(log10(number) + 1) is crucial for calculating the number of digits in the input.

PREREQUISITES
  • Understanding of MATLAB programming and function creation
  • Familiarity with logarithmic functions, specifically log10
  • Basic knowledge of integer data types and significant digits
  • Experience with conditional logic in programming
NEXT STEPS
  • Implement the MATLAB function to truncate numbers using the discussed logic
  • Explore MATLAB's built-in functions for handling numerical precision
  • Learn about error handling in MATLAB functions for invalid inputs
  • Investigate performance optimization techniques for large datasets in MATLAB
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and anyone interested in numerical methods for truncating significant digits in computational applications.

asset101
Messages
10
Reaction score
0
Write a Matlab Function file that takes one integer in and converts it to an integer with only 5 significant digits.
Eg Trunc(123)--------ans=123
Tunc(123345678) ----ans=12345e+03
----------------------------------------------------------------------------------------

My thoughts
First see that the max number that doesn't get truncated is 99999.

set up some logic
if n>99999
insert logic
else
n=n
(n is the value passed into the function).
Can anyone give us a hand?
 
Physics news on Phys.org
HINT: What does taking the base 10 logarithm of a number (in MATLAB, log10) tell you?
 
Taking the base 10 is to find out how many times you would have to multiply by 10 to get the number. As i understand it to be.
I am still a little trouble with getting the missing logic.
Can you push me just a little bit further.
 
asset101 said:
Taking the base 10 is to find out how many times you would have to multiply by 10 to get the number. As i understand it to be.
I am still a little trouble with getting the missing logic.
Can you push me just a little bit further.

Actually, you had the answer, you just phrased it in a manner which isn't so helpful ;-)

Taking the log10 of any number (greater than 1) gives you 1 less than the number of digits in that number.

Consider:
log10(10) = 1
log10(99) = 1.9956

So if you add 1 to log10(number > 1) and then rounded down, you'd get the number of digits in the number the user inputs.

>> digits = floor(log10(number) + 1)

And I think that's about as much as I can give you without giving away everything.

EDIT: Clarification
 
Last edited:
Thanks mate was able to get it in the end sorry about the cross posting, novice user here.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
1
Views
2K
Replies
7
Views
1K
  • · Replies 5 ·
Replies
5
Views
6K