arraysmatlabmatrixbraille

How to "iterate" over submatrixes in matlab


I have 36x80 Matrix in Matlab. It consists of 3x2 arrays which are the symbols of the Braille. i.e.

0 0 0 1 0 1 0 0 .....
0 1 0 0 1 0 0 0 .....
0 1 0 1 0 1 1 1 .....
.....................

Where first 3x2 submatrix represent "p" letter

0 0 
0 1
0 1

Next is "r" and so on. And I have many of these "pattern" 3x2 matrixes which are represent Braille symbols.

How to translate that big matrix into matrix of English characters?


Solution

  • You can convert this matrix to a cell array like:

    Bs = mat2cell(B,repelem(3,size(B,1)/3),repelem(2,size(B,2)/2));
    

    Where B is your original matrix.

    You have to prepare the Braille code in the same manner, so you can compare it to your matrix:

    letters = {'p',[0 0;0 1;0 1];'r',[0 1;0 0;0 1]}; % ...and so on for all letters
    

    Then you can loop over Bs:

    txt = char(zeros(size(Bs))); % the result
    for k = 1:numel(Bs)
        for l = 1:size(letters,1)
            if isequal(Bs{k},letters{l,2})
                txt(k) = letters{l,1};
                break
            end
        end
    end
    

    And here is another option, without converting your matrix to cell array:

    BB = reshape(reshape(B,3,[]),3,2,[]);
    txt = char(zeros(size(B,1)/3,size(B,2)/2)); % the result
    for k = 1:size(BB,3)
        for l = 1:size(letters,1)
            if isequal(BB(:,:,k),letters{l,2})
                txt(k) = letters{l,1};
                break
            end
        end
    end
    

    This should be faster, especially if you have a lot of data.