Search code examples
matlabfor-loopbinarycharactercell-array

Creating a cell array with 2 rows; 1st row for characters and 2nd for their related 5 bits using dec2bin function in matlab


I am trying to make a 2*32 cell array in matlab. The first row are the characters 'a' to 'z' , space, period, comma, exclamation Point, semicolon and quotation (32 chars). The second row must be '00000', '00001', '00010' and so on.

I am supposed to use dec2bin function but I don't know how and I tried to do so by a for loop but it didn't work. Here's my very little initial code:

clc;
clear;
Mapset = cell(2,32);
Mapset = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'...
   'q','r','s','t','u','v','w','x','y','z',' ','.',',','!',';','"'};

for i=0:27
 Mapset(2,i)= dec2bin(i,5)
end

Thanks for your help in advance (I really need it).


Solution

  • clc;
    clear;
    
    % Initialize the cell array with the characters in the first row
    Mapset = cell(2,32);
    Mapset(1,:) = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    
    % Populate the second row with the corresponding 5-bit binary values
    for i=1:32
        Mapset{2,i} = dec2bin(i-1, 5);
    end
    
    % Display the resulting cell array
    disp(Mapset);