MATLAB: I know this is probably a novice question, but I am quite a Matlab novice. The while loop in the script begins to run ridiculously slow as the table “nonapattern” increases in size. Is it possible to increase the speed somehow? Thank you.

performance

counter=1;
searchsize=254;
patternsize=92378;
j=1;
i=1;
newlist = zeros(100,2);
while counter<patternsize
while i<searchsize
i
if isequal(pinellas{i,3},nonapattern{counter,1})
newlist(j,1)=pinellas(i,1);
newlist(j,2)=pinellas(i,2);
j=j+1;
end
i=i+1;
end
counter=counter+1;
i=1;
end
Pattern trajectory is the script which matches the patterns from "Data" with the list in "nonapattern". When "nonapattern" becomes large (e.g. around 90,000 x 2 element table) the script takes days to run. Thanks so much for any suggestions/help to make this run faster.

Best Answer

Looks like the size of the matrix is increasing by each entry. Read about preallocation and preallocation of matrices of unknown size .
Other than that, the original script loops through one cell array, nonapattern, and finds matching strings in a second cell array, data, including duplicates. Some data is then extracted from the matched rows of data. Faster code given below:
Load data
[~,nonapattern]=xlsread('nonapattern.xlsx');
[numdata,data]=xlsread('Data.xlsx');
Find pairs of identical strings in each cell arrays
[C,ia,ib] = intersect(nonapattern,data)
C =
3×1 cell array
{'SO5 SO6 SOA SOB SOC SOD SOE SOG SOO'}
{'SO5 SO6 SOA SOB SOD SOE SOG SOH SOO'}
{'SO5 SO6 SOD SOE SOF SOG SOK SOM SON'}
Next, find duplicates
index=cellfun(@(x)find(ismember(data,x)==1),C,'uniformoutput',false)
index =
1×3 cell array
{5×1 double} {4×1 double} {2×1 double}
Grab corresponding numerical data from numdata, columns 1 and 2
out=cellfun(@(x)numdata(x,1:2),index,'uniformoutput',false);
out =
3×1 cell array
{5×2 double}
{4×2 double}
{2×2 double}