I am trying to solve the following problem and got stuck if you can give some guidance
2 views (last 30 days)
Show older comments
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
Accepted Answer
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 = find_max_sigdec(3.123456789)
disp(y)
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
More Answers (0)
See Also
Categories
Find more on Oil, Gas & Petrochemical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!