How to delete rows while maintaining the indexes?

I'm trying to represent a tree of arrays in a matrix, like this: m(2,:) and m(3,:) are the children of m(1,:), m(4,:) and m(5,:) are the children of m(2,:) and so on.
Now I want to delete some rows from the memory, while maintaining the indexes. For example: I have a matrix m=[1 2 3; 2 4 6; 3 6 9] and I want to delete row 2, so the result is: m=[1 2 3; ; 3 6 9].
Yes, I want the deleted row to count, because I have a lot of indexes pointing to the other rows, but not to consume memory.
I tried m(2,:)=[], but this way I lost the indexe. I mean, row 3 became row 2. Any ideas?

 Accepted Answer

You can't do that with arrays in MATLAB. The contents cannot be "null" but still present for indexing purposes ... they have to be physically present in memory. Your choices are:
1) Keep the original matrix as is
Keep track of the indexing off to the side as you are currently doing
Keep track of the deleted rows off to the side as well
2) Use cell arrays where each row is a cell element.
(allows you to effectively have a "null" row to maintain indexing)
Keep track of the indexing off to the side as you are currently doing
3) Something else ...
Not knowing how you are using this downstream, it is hard to advise which approach I mentioned (or perhaps another approach) would be better for you. How large are these arrays? Why are you worried about memory?

1 Comment

Cell arrays are very good for me. These arrays are tens of thounsands by hundreds large and I don't have that memory. Thank you very much.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!