Re-writing a loop including strcat in order to reduce computational time

3 views (last 30 days)
I have the following loop:
NameMatrix = [];
for i = 1:rows
NameMatrix = [NameMatrix; {strcat(char(64+i),'1')} {strcat(char(64+i),'2')} {strcat(char(64+i),'3')} {strcat(char(64+i),'4')} {strcat(char(64+i),'5')}];
end
Where rows is the number of rows of an input vector and changes size. With this I create rows x 5 cell which for rows = 3 looks like this:
'A1' 'A2' 'A3' 'A4' 'A5'
'B1' 'B2' 'B3' 'B4' 'B5'
'C1' 'C2' 'C3' 'C4' 'C5'
If rows = 5 it would look like this:
'A1' 'A2' 'A3' 'A4' 'A5'
'B1' 'B2' 'B3' 'B4' 'B5'
'C1' 'C2' 'C3' 'C4' 'C5'
'D1' 'D2' 'D3' 'D4' 'D5'
'E1' 'E2' 'E3' 'E4' 'E5'
I profiled my code and I noticed that the function strcat is super slow. How can I re-write the loop so that it produces the same output, but is faster?

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 5 Aug 2015
Edited: Azzi Abdelmalek on 5 Aug 2015
n=5
m=10
s=regexp('A':'Z','\S','match');
out=genvarname(repmat(s(1:n)',1,m),s(1:n))

Community Treasure Hunt

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

Start Hunting!