Please can somebody expain this code.

조회 수: 4 (최근 30일)
Syed Bilal
Syed Bilal 2014년 3월 17일
댓글: Walter Roberson 2025년 2월 3일
tic
M = 'FFFFFFFFFFFFFFFF';
MB=[];
for i=1:16
Mi=M(i);
MBi=['0000',dec2bin(hex2dec(Mi))];
MBi=MBi(end-3:end);
MBi=[str2num(MBi(1)),str2num(MBi(2)),str2num(MBi(3)),str2num(MBi(4))];
end
M=MB
  댓글 수: 1
Kevin Claytor
Kevin Claytor 2014년 3월 17일
Did you have a look at the docs:
They pretty much explain it. If you still don't understand the whole thing, post back with what you do understand and where you're getting stuck. You might also find;
helpful.
I'm disappointed that I can't add the the link markup into code segments. It would have been more fun that way.

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

답변 (1개)

Ishaan
Ishaan 2025년 1월 29일
The code you shared appears to convert a 16-character hexadecimal string into its equivalent binary representation.
Refer to the following code with added comments and some improvements for better understanding.
tic % Start a timer to measure the execution time of the code
M = 'FFFFFFFFFFFFFFFF'; % Initialize a string representing a 16-character hexadecimal number
MB = []; % Initialize an empty array to store binary representations
for i = 1:16 % Loop through each character in the hexadecimal string
Mi = M(i); % Extract the i-th character from the hexadecimal string M
% Convert the hexadecimal character to a decimal number, then to a binary string
MBi = ['0000', dec2bin(hex2dec(Mi))];
% Ensure the binary string is 4 characters long by taking the last 4 characters
MBi = MBi(end-3:end);
% Convert each character in the binary string to a number and store in MBi
MBi = [str2double(MBi(1)), str2double(MBi(2)), str2double(MBi(3)), str2double(MBi(4))];
MB = [MB, MBi]; % Append the binary array to MB
end
M = MB % Assign the final binary representation to M
M = 1×64
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
toc; % End the timer and report the execution time
Elapsed time is 0.131393 seconds.
I noticed 3 potential errors in the code you provided and made these changes.
  1. The result at each intermediate step is not stored and is overridden. Added the line “MB = [MB, MBi];” at the end of the loop to that.
  2. ‘str2num` is not recommended in this case as it is not suitable for converting single character strings to numbers. Instead, you should use str2double or directly convert the character to a number using -'0' which works well for character arrays representing digits.
  3. “tic” was used without “toc”. “tic” marks the start of the timer and “toc” marks its end reporting/returning the time it took for the code to execute between the two. I added "toc" at the end of the code.
I hope that it is now clear what the code snippet you provided does.
  댓글 수: 2
Walter Roberson
Walter Roberson 2025년 2월 3일
dec2bin(hex2dec(Mi),4)
would make more sense then the string manipulation you are doing.
Walter Roberson
Walter Roberson 2025년 2월 3일
Instead of
MBi = [str2double(MBi(1)), str2double(MBi(2)), str2double(MBi(3)), str2double(MBi(4))];
you can use
MBi = MBi - '0';

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by