MATLAB: How to reconstruct a matrix form a vector and a mask matrix

reconstruct a matrix

I am using a mask matrix to reduce the size and remove not relevant part, but I cannot reconstruct the matrix back.
i.e.
a =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
M =
0 0 0 0 0
0 1 1 1 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0
>> B=a(M>0)’
B =
5 6 12 7 13 19 14 20 21
Now what I need is a way to go back i.e
>> A=(??M??B??)
A=
0 0 0 0 0
0 5 7 14 0
0 6 13 20 0
0 12 19 21 0
0 0 0 0 0
thanks

Best Answer

No need for the intermediate step there. Just directly use element-wise multiplication to create A matrix
a = [
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9];
M = [
0 0 0 0 0
0 1 1 1 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0];
A = a.*M
Result
>> A
A =
0 0 0 0 0
0 5 7 14 0
0 6 13 20 0
0 12 19 21 0
0 0 0 0 0