Clear Filters
Clear Filters

I am trying to solve the following problem and got stuck if you can give some guidance

3 views (last 30 days)
Problem 713. Find the maximum number of decimal places in a set of numbers
Find the maximum number of decimal places in a set of numbers after decimal point that are significant
function y = find_max_sigdec(x)
y = max(max(length(rem(x,1))));
end
  3 Comments
dareen
dareen on 16 Mar 2024
Edited: dareen on 16 Mar 2024
what a save i dont understand how i missed this up so bad with length i figured something is wrong but kept changing the elements of a matrix of the same size and some how got really fitting answers 4 and the max was always 4 digits ,so when i used length it gave me the length of the matrix not the elements ,and the elements are not strings they are numebrs whatever i substitue has a length of 1 which is somehow confusing in other languages the length of the number was the num of digits it has so it seems like i have to change the input to a string to be able to deal with this the link you added really made the double precision topic more understandable thanks , i will try to learn more about strigns so that i can reach a solution of my own and just to check
if i want to fix the code is there a simpler approach that you would reccomend
thanks again @Dyuman Joshi

Sign in to comment.

Accepted Answer

Hassaan
Hassaan on 16 Mar 2024
Edited: Hassaan on 16 Mar 2024
Looks like a MATLAB Cody Problem. One of the many approaches that may exist:
x = [1.000 1.04 0.22 10.1;
2.05 2.33 4.1 1000.31;
5.00010 6.429 7.492 8.0];
y = find_max_sigdec(x)
y = 4
%y = find_max_sigdec(3.123456789)
disp(y)
4
function y = find_max_sigdec(x)
maxDecimals = 0; % Initialize the maximum number of decimal places
x = x(:); % Linearize the matrix to iterate over all elements
for i = 1:numel(x)
% Extract the decimal part by subtracting the integer part
decimalPart = abs(x(i) - floor(x(i)));
if decimalPart > 0
% Convert the decimal part to a string
str = num2str(decimalPart);
% Find the index of the decimal point
idx = strfind(str, '.');
% Calculate the number of significant decimal places
numDecimals = length(str) - idx;
% Update maxDecimals if this number is larger
maxDecimals = max(maxDecimals, numDecimals);
end
end
y = maxDecimals; % Return the maximum number of decimal places found
end
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  5 Comments
dareen
dareen on 16 Mar 2024
Edited: dareen on 16 Mar 2024
thank u this was helpful ,and pardon me i will just wait for a while before accepting an answer so that there is a possibility to recive new comments or answers

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!