Any tips on efficiently and quickly deleting a given row/column from a Matrix?
I had initially believed that deleting the last column of a given matrix would be more efficient than the first column, and all column operations would be more efficient than row operations (given MATLAB's column based memory), which I was able to confirm through testing. However, the performance I did get was rather unfortunate.
someB = rand(4,50000);someC = someB.'; tic while size(someB,2) > 2 someB(:,size(someB,2)) = []; end toc tic while size(someC,1) > 2 someC(size(someC,1),:) = []; end toc %Elapsed time is 13.869280 seconds.
%Elapsed time is 10.198270 seconds.
I did a quick search and in this thread I found hope that through external C MEX functions there may indeed be a way to efficiently delete the last column of a matrix quickly. The code is attached below.
#include "mex.h"// You may need to uncomment the next line//#define mwSize intvoid mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ mwSize n; if( n = mxGetN(prhs[0]) ) mxSetN(prhs[0], n - 1);}
However, I was not able to get said code running myself. If you take a quick look at the results that the author was finding, you'll find rather remarkable performance. I'm not that good at MEX myself; would anyone know how to fix above code so that it runs, or alternatively, have an equally/near equally good MEX code/MATLAB code performance-wise?
Thanks!
Best Answer