Please can somebody expain this code.

3 次查看(过去 30 天)
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