Matlab help, I don't know which part is wrong.

조회 수: 1 (최근 30일)
Ryan W
Ryan W 2022년 9월 25일
댓글: Steven Lord 2022년 9월 25일
Trying to write a function to convert a decimal number to a binary number.
function [binary] = myDecimal(decimal)
binary = [];
while decimal > 0
binary = [mod(decimal,2) binary];
decimal = floor(decimal/2);
end
end
%codes to call the function
% Run learner solution.
decimal = 7;
binary = myDecimal(decimal);
% Run reference solution.
binaryReference = reference.myDecimal(decimal);
% Compare.
assessVariableEqual('binary', binaryReference);
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2022년 9월 25일
@Ryan W - which part of your code do you think is incorrect? I don't understand the line
% Run reference solution.
binaryReference = reference.myDecimal(decimal);
which I assume you have so that you can compare a known binary string with the result from your algorithm. Consider using dec2bin which will create a string (so that will mean that you need to change your algorithm to create a string too).
Steven Lord
Steven Lord 2022년 9월 25일
What leads you to believe your solution is incorrect?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2022년 9월 25일
Instead of your function, just use the built-in function dec2bin
binaryNumber = dec2bin(decimalNumber)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by