fill a matrix, with other matrix

I have a matrix ("M") of the size 169x212 with either a value of 0 or 1. Now for every value 0 i want to place in another matrix ("A") with the size of 16x16 And for every value 1, i want to place a different matrix ("B") with the size of 16x16
thus creating a matrix of 2704 x 3392.
How do i get this to work ?

 Accepted Answer

Jos (10584)
Jos (10584) on 31 May 2016
Edited: Jos (10584) on 31 May 2016
A smaller example, but you should be able to get this working in your case:
M = [1 0 1 1 ; 0 1 0 1 ; 1 1 1 0 ; 0 0 1 0]
A = [11 12 ;21 22], B = [1 2 ; 3 4]
C = cell(size(M))
C(M==0) = {A}
C(M==1) = {B}
Mout = cell2mat(C)
or in a one-liner:
Mout2 = cell2mat(arrayfun(@(x) (x==0).*A + (x==1).*B, M, 'un', 0))

More Answers (0)

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!