Please can somebody expain this code.
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Kevin Claytor
il 17 Mar 2014
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.
Risposte (1)
Ishaan
il 29 Gen 2025
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
toc; % End the timer and report the execution time
I noticed 3 potential errors in the code you provided and made these changes.
- 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.
- ‘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.
- “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 Commenti
Walter Roberson
il 3 Feb 2025
dec2bin(hex2dec(Mi),4)
would make more sense then the string manipulation you are doing.
Walter Roberson
il 3 Feb 2025
Instead of
MBi = [str2double(MBi(1)), str2double(MBi(2)), str2double(MBi(3)), str2double(MBi(4))];
you can use
MBi = MBi - '0';
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!