Clear Filters
Clear Filters

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

5 views (last 30 days)
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!

Accepted Answer

Mike Hosea
Mike Hosea on 9 Jul 2013
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
  2 Comments
surya
surya on 9 Jul 2013
Many thanks for your response Mike! I'm using MATLAB R2013a. In particular, I'm using SIMULINK Coder, which seems to be more stringent than MATLAB Coder. I will try the second approach you mentioned. The first one, SIMULINK coder does not accept. Thank you! Surya.
Mike Hosea
Mike Hosea on 9 Jul 2013
Simulink Coder should work with either, but if your MATLAB Function Block has variable-size outputs you have to jump through some hoops to get Simulink to allow a variable-size signal. You'll probably have to set explicit upper bounds for any variable-size signals coming out of a block, and in 13a you will need to check the variable-size signals box on code-generation/interface/ pane of the model configuration parameters.

Sign in to comment.

More Answers (2)

Guru
Guru on 7 Jul 2013
The Embedded MATLAB Fcn is intended for MATLAB that would resemble C code. How do you suppose you can delete rows/columns from a matrix in C?
That is a rhetorical question, but the answer is quite simply you cannot, nor should you be trying to do so. I have never wrote C for Embedded Systems where I had to delete a row/column from a matrix type of functionality. That is only the case where I didn't know enough about my system to not know how much to allocate for every data that I had.
Short answer: You cannot do so, nor should you be able to as it is not what Embedded MATLAB Fcn is intended for.

surya
surya on 7 Jul 2013
Thanks for your response. And let me explain, no i'm not intending to write C-code. I'm writing an embedded matlab function to generate C-code using simulink coder.. There is a way of specifying variable size matrices in MATLAB/Simulink Coder using "coder.varsize" so I can start with a matrix of one size and increase it or decrease its size through the program.
Now an example would be:
coder.varsize('G',[100,100]); G = ones(10); G = G(1:9,1:9);
would give you a matrix G which is initially of size 10x10, but modified to size 9x9.
You might not have come across the need for such cases, but it is possible. I'm just looking for an elegant way to do this over multiple rows and columns.
Hence, the question is not rhetorical.

Community Treasure Hunt

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

Start Hunting!