matrix insert with a condition

2 ビュー (過去 30 日間)
JK
JK 2019 年 5 月 17 日
コメント済み: Guillaume 2019 年 5 月 18 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by