matrix insert with a condition

1 visualización (últimos 30 días)
JK
JK el 17 de Mayo de 2019
Comentada: Guillaume el 18 de Mayo de 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

Respuesta aceptada

Guillaume
Guillaume el 17 de Mayo de 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 comentarios
JK
JK el 17 de Mayo de 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 el 18 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by