Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: strings transformation sequence optimization
Replies: 6   Last Post: May 15, 2014 9:58 AM

 Messages: [ Previous | Next ]
 Michal Kvasnicka Posts: 70 Registered: 5/6/14
strings transformation sequence optimization
Posted: May 14, 2014 2:20 PM

I have the following problem:

I need to transform encoded integer sequences c to string messages m by following association:
numpos = 4
c = [1 3 2 2, 3 2 2 1], 2*numpos integer parameters, where first numpos parameters encoded position of types = {'a' 'b@x' 'c@y'} and second numpos parameters are applied only if type conmtains character '@' like this:
m = 'a c:2@y b:2@x b:1@x'

My code is very ineffective, because more than 80% of time spent on lines:
if ismember('@',fan)
and
fansplit = strsplit(fan,'@');

for number of lines about 1000 and more is very cpu time consuming.

So, my question is: How to optimize (vectorize) this code to significantly gain computation speed? Target values are: numpos = 30, numlines = 4000
===================================================
% input data
numpos = 7;
numlines = 10;
c = repmat([3 4 1 1 4 2 5,1 1 1 1 2 2 2],numlines,1);
types = {'a' 'b@2' 'c@6' 'd@10' 'e@11'};

% number of lines
nl = size(c,1);
% message vectors declaration
m = cell(nl,1);
% loop over lines of c
for i = 1:nl
% loop over items of types
for j = 1:numpos
% current j-th type name in i-th line
fan = types{c(i,j)};
if ismember('@',fan)
% @ character type case
fansplit = strsplit(fan,'@');
m{i} = [m{i} ' ' fansplit{1} ':' num2str(c(i,numpos+j)) '@' fansplit{2}];
else
% non-@ character type case
m{i} = [m{i} ' ' fan];
end
end
end

Date Subject Author
5/14/14 Michal Kvasnicka
5/14/14 Bruno Luong
5/15/14 Michal Kvasnicka
5/15/14 Michal Kvasnicka
5/15/14 Bruno Luong
5/15/14 Michal Kvasnicka
5/15/14 Bruno Luong