How to keep the values for particular dimension of a matrix and rest of the dimension will be zeros?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
AS
el 27 de Sept. de 2023
Comentada: AS
el 29 de Sept. de 2023
I have a matrix A with dimension 496 by 48. I want to keep the values with dimension A(350:400, 20:35) as it is and rest of the dimension of A will be zeros.
0 comentarios
Respuesta aceptada
Walter Roberson
el 27 de Sept. de 2023
Easy way:
B = zeros(size(A), 'like', A);
B(350:400,20:35) = A(350:400,20:35);
Now B is A with all the other elements zeroed.
A different approach:
mask = true(size(A));
mask(350:400,20:35) = false;
A(mask) = 0;
A third approach:
A(1:349,:) = 0; A(401:end,:) = 0;
A(:,1:19) = 0; A(:,36:end) = 0;
Más respuestas (1)
Bruno Luong
el 27 de Sept. de 2023
A single line solution
A(~(ismember(1:size(A,2),350:400) & ismember((1:size(A,1))',20:35))) = 0
Ver también
Categorías
Más información sobre Matrix Decomposition 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!