extract all rows of a matrix except 'r' (vector) rows
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alberto Acri
el 8 de Sept. de 2023
Respondida: MarKf
el 8 de Sept. de 2023
Hi! I need to extract all rows of 'matrix' except 'r' rows.
matrix = [54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65];
r = [1; 2; 3; 4: 9];
The result is:
matrix_out = [58; 59; 60; 61; 63; 64; 65];
0 comentarios
Respuesta aceptada
Stephen23
el 8 de Sept. de 2023
The most efficient approach:
matrix = [54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65];
r = [1; 2; 3; 4; 9];
matrix_out = matrix;
matrix_out(r) = []
0 comentarios
Más respuestas (2)
Dyuman Joshi
el 8 de Sept. de 2023
Editada: Dyuman Joshi
el 8 de Sept. de 2023
matrix = [54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65];
r = [1; 2; 3; 4; 9];
%Method 1
%remove directly
matrix(r,:)=[];
%or
%Method 2
%remove indirectly
idx = 1:size(matrix,1);
ix = setdiff(idx,r);
matrix = matrix(ix,:);
Note that Method 1 won't work when there is an index not in the range of the dimensions of the array matrix.
For such cases, use Method 2.
0 comentarios
MarKf
el 8 de Sept. de 2023
mat = magic(8)
r_not = [1 3:5 8];
mat(r_not,:) = [] %use mat_o = mat; to keep the original mat;
There are some issues with your example:
matrix = [54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65];
r = [1:3, 6, 9]; %btw this did not work with those dimensions;
% also not the way you wanted with those indices (matrix_out just removed 1:3)
matrix_out = matrix;
matrix_out(r,:) = []
Since yours is a simple vector, matrix_out(r) = []; would have worked easily too
0 comentarios
Ver también
Categorías
Más información sobre Resizing and Reshaping 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!