matrix insert with a condition

1 Ansicht (letzte 30 Tage)
JK
JK am 17 Mai 2019
Kommentiert: Guillaume am 18 Mai 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

Akzeptierte Antwort

Guillaume
Guillaume am 17 Mai 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 Kommentare
JK
JK am 17 Mai 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 am 18 Mai 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by