Hi, I have a char matrix of hex values that I need to convert to 4-bit binary values. Each entry in the hex matrix is a single letter or number. I have then written some code that takes each entry from the hex matrix and creates 4 entries in the binary matrix.
My problem is that it doesn't work properly, but it did at one point. I must've made some small change that I now can't remember, because the x matrix that should be a copy of HexMat is not a copy, and my BinaryMat consists of 48s and 49s instead of 1s and 0s.
Here's the basic format for my if statements:
if(HexMatrix(coordinates) == 'value' BinaryMatrix(1) = '1st binary digit corresponding to value'; BinaryMatrix(2) = '2nd binary digit corresponding to value'; BinaryMatrix(3) = '3rd binary digit corresponding to value'; BinaryMatrix(4) = '4th binary digit corresponding to value'; x(coordinates) = value;
Here's the code:
HexMat = ['1','2','0';'A','C','8';'9','6','F']; %Hex Matrix definition
[m,n] = size(HexMat); %My actual matrix can change size based on some dataset which is why I'm not using constants.
%Initialize the binary matrix for speed, and I know it'll be 4 times as
%wide as the hex matrix since I'm using 4-bit binaries.
BinaryMat = zeros(m,n*4);%I'm also initializing an x matrix which *should* just be a copy of HexMat
x = zeros(m,n);for w = 1:m for v = 1:n if isequal(HexMat(w,v), '0') BinaryMat(w,v*4-3) = '0'; BinaryMat(w,v*4-2) = '0'; BinaryMat(w,v*4-1) = '0'; BinaryMat(w,v*4) = '0'; x(w,v) = '0'; elseif isequal(HexMat(w,v), '1') BinaryMat(w,v*4-3) = '0'; BinaryMat(w,v*4-2) = '0'; BinaryMat(w,v*4-1) = '0'; BinaryMat(w,v*4) = '1'; x(w,v) = '1'; elseif isequal(HexMat(w,v), '2') BinaryMat(w,v*4-3) = '0'; BinaryMat(w,v*4-2) = '0'; BinaryMat(w,v*4-1) = '1'; BinaryMat(w,v*4) = '0'; x(w,v) = '2'; elseif isequal(HexMat(w,v), '3') BinaryMat(w,v*4-3) = '0'; BinaryMat(w,v*4-2) = '0'; BinaryMat(w,v*4-1) = '1'; BinaryMat(w,v*4) = '1'; x(w,v) = '3'; elseif isequal(HexMat(w,v), '4') BinaryMat(w,v*4-3) = '0'; BinaryMat(w,v*4-2) = '1'; BinaryMat(w,v*4-1) = '0'; BinaryMat(w,v*4) = '0'; x(w,v) = '4'; elseif isequal(HexMat(w,v), '5') BinaryMat(w,v*4-3) = '0'; BinaryMat(w,v*4-2) = '1'; BinaryMat(w,v*4-1) = '0'; BinaryMat(w,v*4) = '1'; x(w,v) = '5'; elseif isequal(HexMat(w,v), '6') BinaryMat(w,v*4-3) = '0'; BinaryMat(w,v*4-2) = '1'; BinaryMat(w,v*4-1) = '1'; BinaryMat(w,v*4) = '0'; x(w,v) = '6'; elseif isequal(HexMat(w,v), '7') BinaryMat(w,v*4-3) = '0'; BinaryMat(w,v*4-2) = '1'; BinaryMat(w,v*4-1) = '1'; BinaryMat(w,v*4) = '1'; x(w,v) = '7'; elseif isequal(HexMat(w,v), '8') BinaryMat(w,v*4-3) = '1'; BinaryMat(w,v*4-2) = '0'; BinaryMat(w,v*4-1) = '0'; BinaryMat(w,v*4) = '0'; x(w,v) = '8'; elseif isequal(HexMat(w,v), '9') BinaryMat(w,v*4-3) = '1'; BinaryMat(w,v*4-2) = '0'; BinaryMat(w,v*4-1) = '0'; BinaryMat(w,v*4) = '1'; x(w,v) = '9'; elseif isequal(HexMat(w,v), 'A') BinaryMat(w,v*4-3) = '1'; BinaryMat(w,v*4-2) = '0'; BinaryMat(w,v*4-1) = '1'; BinaryMat(w,v*4) = '0'; x(w,v) = 'A'; elseif isequal(HexMat(w,v), 'B') BinaryMat(w,v*4-3) = '1'; BinaryMat(w,v*4-2) = '0'; BinaryMat(w,v*4-1) = '1'; BinaryMat(w,v*4) = '1'; x(w,v) = 'B'; elseif isequal(HexMat(w,v), 'C') BinaryMat(w,v*4-3) = '1'; BinaryMat(w,v*4-2) = '1'; BinaryMat(w,v*4-1) = '0'; BinaryMat(w,v*4) = '0'; x(w,v) = 'C'; elseif isequal(HexMat(w,v), 'D') BinaryMat(w,v*4-3) = '1'; BinaryMat(w,v*4-2) = '1'; BinaryMat(w,v*4-1) = '0'; BinaryMat(w,v*4) = '1'; x(w,v) = 'D'; elseif isequal(HexMat(w,v), "E") BinaryMat(w,v*4-3) = '1'; BinaryMat(w,v*4-2) = '1'; BinaryMat(w,v*4-1) = '1'; BinaryMat(w,v*4) = '0'; x(w,v) = 'E'; elseif isequal(HexMat(w,v), 'F') BinaryMat(w,v*4-3) = '1'; BinaryMat(w,v*4-2) = '1'; BinaryMat(w,v*4-1) = '1'; BinaryMat(w,v*4) = '1'; x(w,v) = 'F'; else w %I want these to display, hence the missing ;
v er = HexMat(w,v) error('Hexadecimal mismatch error @ coordinates [w,v] of HexMat, defined as variable er') end endend
The v*4-(3,2,1,0) stuff is because for each entry in HexMat I'll have 4 entries in BinaryMat. My 2nd column in HexMat will correspond to the 2*4-3 (5th), 2*4-2 (6th), 2*4-1 (7th), and 2*4 (8th) columns in BinaryMat. Any help is appreciated, I think I'm overlooking something simple here like some formatting or something. Like I said, it was working earlier today and I thought I only edited it to make comments for clarity, but I must've changed something in the code itself as well.
Best Answer