matrix insert with a condition

1 次查看(过去 30 天)
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

采纳的回答

Guillaume
Guillaume 2019-5-17
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 个评论
JK
JK 2019-5-17
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 2019-5-18
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by