MATLAB: How to delete rows/columns of a matrix in Embedded Matlab Fcn

codermatlab codersimulink

I'm trying to remove specific set of rows and columns of a matrix. In MATLAB, i can just assign them to '[]'. I hope I cannot do that in #codegen ? Please help. Thanks!

Best Answer

I don't know what version you have. MATLAB Coder does currently support deleting rows and columns from a matrix. You will need to start with a variable-size matrix, of course. Use coder.varsize to make any matrix that looks like a fixed-size matrix into a variable-sized one.
You can write something like:
z(rowstodelete,:) = [];
z(:,colstodelete) = [];
where rowstodelete and colstodelete are vectors of row and column indices, respectively. To do both rows and columns at once, I might rather write code like this
ONE = int32(1);
rowsleft = setdiff(ONE:size(z,1),rowstodelete);
colsleft = setdiff(ONE:size(z,2),colstodelete);
z = z(rowsleft,colsleft);
I have to warn you of one thing, however. You will not be able to coax it into making that last assignment an in-place operation. We're working on it. :) -- Mike
Related Question