Can I replace this nested for loop with a faster process?

1 view (last 30 days)
I have a variable, A, which is a Nx1 cell array, where N is a variable number which can vary between 1 and 1000. Each row within A contains another large(ish) cellarray, which can vary from anywhere between 100 and over 1000 rows. Example:
>> A
A =
{583x20 cell}
{371x20 cell}
{626x20 cell}
{450x20 cell}
Column 19 within each row of A contains a set of times which have been converted using datenum, e.g.
>> [A{1,1}{1:5,19}]'
ans =
735965.791900579
735965.791946875
735965.791993171
735965.792039468
735965.792085764
I have written the following code to examine each row of column 19, within each row of matrix A, and make a comparison against some existing values, which uses a nested for loop. I think this is probably quite inefficient, especially if matrix A contains 1000 rows. How can I optimise this code, or replace it with several functions to speed this process up?
metdata_boundaries = datenum({'07:37:30' '10:40:29.99'...
'10:40:30' '13:43:29.99'...
'13:43:30' '16:46:29.99'...
'16:46:30' '19:49:29.99'...
'19:49:30' '22:52:29.99'});
for i = 1:size(A,1)
A{i,1}(:,19) = num2cell(datenum(A{i,1}(:,13))); % Column 13 contains times in the format 'HH:MM:SS.MS'
for x = 1:size(A{i,1},1)
if A{i,1}{x,19} >= metdata_boundaries(1) && A{i,1}{x,19} <= metdata_boundaries(2)
A{i,1}{x,20} = 'Use 09:09 met data';
met0909 = 'True';
elseif A{i,1}{x,19} >= metdata_boundaries(3) && A{i,1}{x,19} <= metdata_boundaries(4)
A{i,1}{x,20} = 'Use 12:12 met data';
met1212 = 'True';
elseif A{i,1}{x,19} >= metdata_boundaries(5) && A{i,1}{x,19} <= metdata_boundaries(6)
A{i,1}{x,20} = 'Use 15:15 met data';
met1515 = 'True';
elseif A{i,1}{x,19} >= metdata_boundaries(7) && A{i,1}{x,19} <= metdata_boundaries(8)
A{i,1}{x,20} = 'Use 18:18 met data';
met1818 = 'True';
elseif A{i,1}{x,19} >= metdata_boundaries(9) && A{i,1}{x,19} <= metdata_boundaries(10)
A{i,1}{x,20} = 'Use 21:21 met data';
met2121 = 'True';
else
A{i,1}{x,20} = 'No met data available';
end
end
end
if exist('met0909','var')
load('metdata_0909.mat')
end
if exist('met1212','var')
load('metdata_1212.mat')
end
if exist('met1515','var')
load('metdata_1515.mat')
end
if exist('met1818','var')
load('metdata_1818.mat')
end
if exist('met2121','var')
load('metdata_2121.mat')
end

Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!