How to create a container with the element in which can be deleted, using matlab?

2 views (last 30 days)
I need a container in which the element can be deleted freely. But the array in matlab only allows us to add element into it, we can't delete a row or column from an array or matrix. So I want to know if this can be easily done using some developed tools in matlab, or how to call the list head file in c++ through matlab? Thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Mar 2012
If you are referring to 2D arrays, then
YourArray(5,:) = [];
would delete row 5, and
YourArray(:,7) = [];
would delete column 7.
If you are referring to arrays with more than 2 dimensions then it is true that you cannot delete individual rows or columns in those arrays. In such a situation, though, you need to define what you want to have happen.
If I delete the upper-left-side row of a cube, then should a "hole" be left where it was, or should (as is the case for regular MATLAB arrays) something "move up" to fill that hole? If something should move, then what? At some point you would be left with a "hole" even if things moved, so what should happen if that hole is accessed? For example, is sum() of that "hole" 0 or the empty set?
The closest that MATLAB has to a list is cell arrays. Lists are, however, often circular or self-referencing, and cell arrays never are.
There is the MATLAB File Exchange contribution by Bruno that gives some (fragile!) pointer capabilities: see http://www.mathworks.com/matlabcentral/fileexchange/24576-inplacearray-a-semi-pointer-package-for-matlab

More Answers (0)

Community Treasure Hunt

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

Start Hunting!