How do I take the determinant of a matrix with one row and one column removed withOUT defining an intermediate matrix?

2 views (last 30 days)
It's easy to remove selected rows or columns of a matrix by defining them to be the empty matrix. But the "removal" operation requires intermediate steps if the reduced matrix is to be subsequently used. In addition, listing the rows or columns I wish to NOT delete is inconvenient when I wish to focus on rows and columns I DO wish to delete. To make my question concrete, suppose I have a square matrix A and I wish to delete two rows and two columns and define the resulting matrix for later use. One way to do this is to retain all of the rows and columns that I do NOT wish to remove. This is awkward for various reasons, for example, if one of the rows or columns to be deleted is the first or last row or column, or if the list of rows or columns is long. It would be much easier to do something like Ared = A(~[row1 row2],~[col1 col2]) where "~" simply means "delete these guys". Then Ared is immediately ready for subsequent use. So my question is: Is there something like "Ared = A(~[row1 row2],~[col1 col2])" available in Matlab?
  2 Comments
Matt J
Matt J on 10 Mar 2014
Edited: Matt J on 10 Mar 2014
This is awkward for various reasons, for example, if one of the rows or columns to be deleted is the first or last row or column, or if the list of rows or columns is long
I understand why it's awkward if "the list of rows or columns is long", but not what difference it makes if the column/row to be deleted is the first or last row. If you want to keep all but the first and last row/column, you would just do this
Ared=A(2:end-1,2:end-1);
Dennis
Dennis on 10 Mar 2014
This is fine if I know that I am deleting the first or last row or column. But suppose I wish to delete rowi and colj but for any given iteration I dont know whether rowi or colj represents the first or last row or column. Then I am not clear on how to use "end" in a way that handles all possible cases.

Sign in to comment.

Answers (3)

dpb
dpb on 10 Mar 2014
Edited: dpb on 10 Mar 2014
Not specifically such a syntax, no; and even if there were it would generate the copy anyway so I think you may as well just generate the reduced matrix directly--I'd expect performance at least as good and often better as I think explicitly writing it probably gives the optimizer more shot at doing what it can.
The only thing I can think of at the moment to write what you're asking would be at the cost of a logical array of the same size that is T for all but the desired row/column. I don't see it as an advantage over the explicit copy route.
ADDENDUM:
...the "removal" operation requires intermediate steps if the reduced matrix is to be subsequently used."
I'd note there that that's only true iff you also need the original subsequently. Otherwise, the deletion in place is the obvious (to me, anyway) choice...
A(rows,cols)=[]; % in place; no intermediate steps
Now, granted, if one is doing multiple passes and deleting different subsets this isn't so good; then one would need to save the deleted rows/columns and reinsert prior to a subsequent deletion. Whether this would or would not be better performing overall would have to be empirically determined for a typical problem.
  1 Comment
Dennis
Dennis on 10 Mar 2014
Yes, I wish to keep the original matrix for subsequent use. I was hoping to take the determinant of the reduced matrix in one fell swoop. I think I see another response that has a solution...... thanks

Sign in to comment.


John D'Errico
John D'Errico on 10 Mar 2014
Not exactly as simple as you asked, but close.
Ared = A(setdiff(1:nrows,[row1 row2]),setdiff(1:ncols,[col1 col2]));
  1 Comment
Dennis
Dennis on 10 Mar 2014
Ahhhhh, I think this is what I was looking for! The "setdiff" keeps rows and columns that are NOT listed as the "deleted ones". Thanks!

Sign in to comment.


Matt J
Matt J on 10 Mar 2014
Edited: Matt J on 10 Mar 2014
So my question is: Is there something like "Ared = A(~[row1 row2],~[col1 col2])" available in Matlab?
I think you need to make it clearer why you don't like this:
Ared=A;
Ared([row1 row2],[col1 col2])=[];
Is it the extra command Ared=A? Don't see how that's a big inconvenience. It costs virtually nothing in CPU time or memory allocation.
Also, to elaborate on dpb's point about logical indexing, there is
Ared=A(~somerows,~somecolumns)
where somerows, somecolumns are logical vectors indicating what you want to delete. 90% of the time in MATLAB, indices start out in logical array form, so I wonder if your problem is that you simply threw away by accident the somerows, somecolumns logical data that [row1,row2] and [col1,col2] originally came from.
  2 Comments
Dennis
Dennis on 10 Mar 2014
Thanks, this is really useful. I did not know what I could delete ANY combination of rows and columns in one fell swoop-------I had thought that only a collection of rows or only a collection of columns could be deleted at one time. Thanks for this new trick!

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!