Hi, I have a very large matrix, and I would like to delete rows at specific intervals.
For example, I have the following matrix : [ 1 2 ; 3 4 ; 5 6 ; 7 8; 9 10; 11 12; 13 14; 15 16 ;17 18; 19 20; 21 22 ; 23 24]
I would like to delete every fourth row so the matrix looks like: [ 1 2; 3 4 ; 5 6; 9 10 ; 11 12; 13 14; 17 18; 19 20; 21 22 ]
Any help would be greatly appreciated.
Thanks,
Seamus

1 comentario

Cristobal Silva
Cristobal Silva el 10 de Ag. de 2015
You could select individually which rows you want, like
A = [ 1 2 ; 3 4 ; 5 6 ; 7 8; 9 10; 11 12; 13 14; 15 16 ;17 18; 19 20; 21 22 ; 23 24]
B = A([1:3,5:7,9:11],:)
Of course, for large data this would be impractical, so you can use a for loop for such task combined with mod operator and vertical concatenation
A = [ 1 2 ; 3 4 ; 5 6 ; 7 8; 9 10; 11 12; 13 14; 15 16 ;17 18; 19 20; 21 22 ; 23 24];
B = zeros(0,2);
for i=1:length(A)
if (mod(i,4) ~= 0)
B = vertcat(B,A(i,:));
end
end
In other words, we create an empty 0-by-2 matrix (because A uses 2 cols per row) and then we just push to that matrix each row that we want to. In this case, we push every row that is not a multiple of 4 (using the mod function).

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Ag. de 2015

1 voto

A(4:4:end,:) = [];

3 comentarios

Seamus
Seamus el 10 de Ag. de 2015
Hi Walter, thanks very much for this. I may be incorrect but I think this extracts out every fourth row from the original matrix. In this instance I'm just looking to delete every fourth row, and leave the remainder.
Walter Roberson
Walter Roberson el 10 de Ag. de 2015
Editada: Image Analyst el 10 de Ag. de 2015
No, extracting every 4th row would be
every4thRow = A(4:4:end,:)
on the right hand side. When you assign [] to an array element, that means to delete the element.
Seamus
Seamus el 11 de Ag. de 2015
My apologies, I read it wrong. That's perfect, thank you very much.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Ag. de 2015

Comentada:

el 11 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by