Thursday, 15 April 2010

Matlab - Select rows given a condition -



Matlab - Select rows given a condition -

i have cell variable (size:2639516x12, bytes:3863876744, class:cell) , want create selection, considering first row. instance if have

a: 1997 fd 89 1997 gd 65 1999 fdk 87 2010 uy 123

i get

b: 1997 fd 89 1997 gd 65

to cell utilize next code:

% transfer csv file matlab data_file = fopen('data.csv'); info = textscan(data_file,'%q %q %q %q %f %f %f %f %s %f %f %f %s %f %s %f %s %f %f %f %s','delimiter',',','headerlines', 1); fclose(data_file); %convert numbers strings f_5=data{:,5}; f_6=num2cell(data{:,6}); f_7=num2cell(data{:,7}); %get first 4 numbers within variable f_5 f_5a=max(0,fix(log10(f_5)+1)-4); f_5b=fix(f_9./10.^f_5a); %convert number string f_5c = num2cell(f_5b); %create new cell w/ variables want a=[f_5c data{:,1} data{:,2} data{:,3} data{:,4} f_6 f_7];

using logical indexing

b = a(cell2mat(a(:,1))==1997,:);

thanks excaza mentioning values may not rounded

if year values not rounded (i.e cells have value 1996.999999 or 1997.0001) utilize

e = 0.001 %\\some little value b = a(abs(cell2mat(a(:,1))-1997)<e,:);

matlab select cell condition

No comments:

Post a Comment