Deleting rows in a matrix
Mostrar comentarios más antiguos
Hi,
I have two matrices which are generated in a loop for n years:
A (m-rows x n-columns) B (k-rows)
Matrix B includes values which represent row numbers of matrix A that should be deleted. So, I want for every value in matrix B, the corresponding row in matrix A to be deleted.
For example,
A=[1 2; 2 2; 3 4], B= [2]
The result should be a matrix [1 2;3 4]. I.e. the middle row is deleted as B includes value 2.
For some reason I'm not able to find the correct coding for this. I tried several things, but inside the loop it doesn't work. This is what I tried, but this does't work:
for j= 1:n
B;
for i= 1:length(B)
indices = find(A(:,1)==B(i));
A(indices,:) = [];
end
end
Can anyone help me with this?
2 comentarios
James Tursa
el 11 de Ag. de 2015
Does B really contain the row numbers, or does it contain values that must match values in the first column of A? Your words say the former but the code looks like the latter.
Bas
el 11 de Ag. de 2015
Respuesta aceptada
Más respuestas (2)
James Tursa
el 11 de Ag. de 2015
Editada: James Tursa
el 11 de Ag. de 2015
"Matrix B includes values which represent row numbers of matrix A that should be deleted"
Assuming this is exactly what you want, then this will do the job:
A(B,:) = [];
Your code produces the result you expect. You'll need to better specify your problem. You can lose the loop completely, though, with a simple indexing:
A(B,:) = [];
This is assuming that B is unique and contains no elements larger than the length of A.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
