Я попытался векторизовать код. Хитрость заключается в том, чтобы
- преобразования таблиц> ячейки> голец-массив, то
- манипулировать полукокса строк, а затем
- преобразовать обратно из полукокса массива> клетка> таблица
Кроме того, является важным битом для заполнения всех ячеек, имеющих более короткие буквы с нулевым символом в векторном виде. Без этого невозможно будет преобразовать из cell> char-array. Вот код. CLC очистить все
%% create Table T
d={'11.01.2016';
'10/19/16';
'12.02.16'};
t={'00:00:00';
'05:29:00';
'06:40'};
dat=[123;
456;
789];
T = table(d,t,dat);
%% deal with dates in Table T
% separate date column and convert to cell
dd = table2cell(T(:,1));
% equalize the lengths of all elements of cell
% by padding 'null' in end of shorter dates
nmax=max(cellfun(@numel,dd));
func = @(x) [x,zeros(1,nmax-numel(x))];
temp1 = cellfun(func,dd,'UniformOutput',false);
% convert to array for vectorized manipulation of char strings
ddd=cell2mat(temp1);
% replace the separators in 3rd and 6th location with '.' (period)
ddd(:,[3 6]) = repmat(['.' '.'], length(dd),1);
% find indexes of shorter dates
short_year_idx = find(uint16(ddd(:,nmax)) == 0);
% find the year value for those short_year cases
yy = ddd(short_year_idx,[7 8]);
% replace null chars with '20XX' string in desirted place
ddd(short_year_idx,7:nmax) = ...
[repmat('20',size(short_year_idx,1),1) yy];
% convert char array back to cell and replace in table
dddd = mat2cell(ddd,ones(1,size(d,1)),nmax);
T(:,1) = table(dddd);
%% deal with times in Table T
% separate time column and convert to cell
tt = table2cell(T(:,2));
% equalize the lengths of all elements of cell
% by padding 'null' in end of shorter times
nmax=max(cellfun(@numel,tt));
func = @(x) [x,zeros(1,nmax-numel(x))];
temp1 = cellfun(func,tt,'UniformOutput',false);
% convert to array for vectorized manipulation of char strings
ttt=cell2mat(temp1);
% find indexes of shorter times (assuming only ':00' in end is missing
short_time_idx = find(uint16(ttt(:,nmax)) == 0);% dirty hack, as null=0 in ascii
% replace null chars with ':00' string
ttt(short_time_idx,[6 7 8]) = repmat(':00',size(short_time_idx,1),1);
% convert char array back to cell and replace in table
tttt = mat2cell(ttt,ones(1,size(t,1)),nmax);
T(:,2) = table(tttt);
Это работает намного быстрее, чем мой код. Большое спасибо. – user2760995
Тем не менее, мне пришлось внести некоторые корректировки, чтобы соответствовать им. Тем не менее, мне пришлось внести некоторые корректировки в соответствии с моими потребностями: Найти формат даты и времени месяца и месяца usDates = ddd (:, 3) == '/ «; ddd (usdates, [1 2 4 5]) = ddd (usDates, [4 5 1 2]); Я должен был установить nmax на 10 и 8 соответственно. Предлагаемый расчет не удался, когда присутствовали только двузначные годы или только элементы, не отображаемые во втором. Я удалил вызовы, чтобы найти, потому что логическое индексирование немного быстрее. Объединение двух массивов символов в одном: tttt = mat2cell (tttt, ones (1, size (raw, 1)), nmaxTimes + nmaxDates + 1); – user2760995
Да, его можно настроить. Из приведенного вами примера я просто показал, как преобразовывать данные из таблицы в структуру массива (что более легко векторизовать). – Abhinav