How can I reconstruct a matrix form a vector and a mask matrix

2 views (last 30 days)
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 31 Oct 2020
Edited: Ameer Hamza on 31 Oct 2020
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
  4 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!