Removing ceratin columns from matrices

Hello, I have code that has matrix K which is a 6x6 matrix. I want to remove certain columns from the K matrix to build a new matrix, Kca. The problem is it keeps updating Kca every time it removes a certain column, causing the next column to be removed the one I don't want or is out of bounds. Any ways around this? I've include my code.

 Respuesta aceptada

James Tursa
James Tursa el 10 de Oct. de 2016
Editada: James Tursa el 10 de Oct. de 2016
If the indexes you want removed are in sorted ascending order, you could probably just reverse the order of your for-loop indexing. E.g.,
for i=1:Ny
would become
for i=Ny:-1:1
That way you delete stuff from the back end first and the indexing doesn't get screwed up for the next iteration. If the indexing you want deleted is not in ascending sorted order, then sort it first.
Note that this incremental deletion causes a data copy at each iteration. For your small matrix this won't matter, but if the matrix is large this can be a huge performance drag. In that case it would be better to delete all of the stuff at once instead of incrementally.

Más respuestas (1)

Walter Roberson
Walter Roberson el 10 de Oct. de 2016

0 votos

Remove the columns starting from the last one and moving toward the first.

Categorías

Preguntada:

TS
el 10 de Oct. de 2016

Comentada:

TS
el 10 de Oct. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by