Removing Rows From a Matrix by Label Quickly
2 visualizaciones (últimos 30 días)
I currently have some code that is destined to: import data as a matrix from a text file, read the data and delete any rows in the matrix that do not start with "H", delete the H label from the remaining rows, print this matrix to a text file. My code is as follows:
FID = fopen('Test_Data_2.txt','rt');
m = [m;str2num(l(2:end))];
FID = fclose(FID);
dlmwrite('md_msd.out', m, 'delimiter', '\t', ...
This code works great for small data sets but I am going to have to use it for sets of 100000 rows or more. I need a way to speed up the process as my current code takes far too long. Is there any way I can make this code faster?
Walter Roberson el 26 de Jun. de 2013
Use other tools when it makes sense to do so. For example, in Linux or OS-X from their shells:
sed -e '/^$|^[^H]/d', -e 's/^H//p' < Test_Data_2.txt > md_msd.out
perl -e '/^H/ && s/^H// && print' < Test_Data_2.txt > md_msd.out
You can invoke perl from within MATLAB using the perl() command.