tags:

views:

130

answers:

3

Is there an inbuilt function that removes the Kth row and column of a square matrix in Matlab?

Hope it's clear from the diagram:

alt text

+1  A: 

Not a builtin function, but the following line does the trick:

y = [x(1:(k-1),1:(k-1)) x(1:(k-1),(k+1):end) ; x((k+1):end,1:(k-1)) x((k+1):end,(k+1):end)];
mtrw
+7  A: 

Here are two simple solutions:

x([1:k-1 k+1:end],[1:k-1 k+1:end])

or:

x(k,:)=[];x(:,k)=[];
Ramashalanka
@Ramashalanka - your first line is much more elegant than mine. I feel silly I didn't see that myself.
mtrw
+2  A: 

If you want to use this operation more often, creating a function is a good idea.

% filename: removeK.m

function M1 = removeK (M, k)
  M1 = M([1:k-1 k+1:end],[1:k-1 k+1:end]);
end

related questions