Search code examples
matlabmatrixbinarybinary-matrixbinary-image

Element-wise binary value concatenation of two matrices


Element by element, I want to concatenate binary values from different matrices to one matrix.

For example,

|1 0 0|  |0 1 0|   |10 01 00|         
|0 1 1|  |1 1 0| = |01 11 10|       
|1 0 1|  |0 0 1|   |10 00 11|

How can this be done?


Solution

  • Here is one alternative without bin2dec or dec2bin conversion

    out = arrayfun(@(x,y) strcat(num2str(x),num2str(y)),A1,A2,'Uni',0);
    

    Input:

    A1 = [1 0 0; 0 1 1; 1 0 1];
    A2 = [0 1 0; 1 1 0; 0 0 1];
    

    output:

    >> out
    
    out = 
    
    '10'    '01'    '00'
    '01'    '11'    '10'
    '10'    '00'    '11'
    

    If you want it as numeric instead of string, you might do this:

    outp = cellfun(@(x) str2double(x), out);
    

    output:

    >> outp
    
    outp =
    
    10     1     0
     1    11    10
    10     0    11