matrix insert with a condition

1 view (last 30 days)
JK
JK on 17 May 2019
Commented: Guillaume on 18 May 2019
can you help me insert m x n matrix into M x N matrix with a condition that is without zero.
starting location is (x,y)
M x N =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
m x n =
0 2 2 0
2 2 2 2
0 2 2 0
(x,y) = (4,3)
M x N =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 2 2 1 1 1 1
1 1 1 2 2 2 2 1 1 1
1 1 1 1 2 2 1 1 1 1

Accepted Answer

Guillaume
Guillaume on 17 May 2019
A = [1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1];
B = [0 2 2 0
2 2 2 2
0 2 2 0];
row = 3; col = 4;
toinsert = B + ~B .* A(row:row+size(B, 1)-1, col:col+size(B, 2)-1);
A(row:row+size(B, 1)-1, col:col+size(B, 2)-1) = toinsert
  2 Comments
JK
JK on 17 May 2019
thanks for your feedback.
I am looking for a solution with a larger matrix which unfortunately i can't do all the individual calculations.
Guillaume
Guillaume on 18 May 2019
What individual calculations?
The code above is completely generic and will work for any size A and B and wherever any zeros are in B.
If it doesn't work for your generic case, then you need to explain a lot better what it is you want.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating 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!