I am trying to solve the following problem and got stuck if you can give some guidance
2 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Hassaan
2024-3-16
编辑:Hassaan
2024-3-16
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Oil, Gas & Petrochemical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!