MATLAB: Improve performance in ‘expansion of data’ function

bottleneckperformancevector expansion

Hi,
In my application i have a performance bottleneck in the following function. I have experimented a little and improved it a bit but im wondering if it can be made any faster. Its right on the edge of acceptable and too slow still.
I need a function to do the following:
if I = [1 2 3 4] then expanding it by 4 would give [1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4]
I am using the data generated from the code below to apply to another set of data
%Expand I and Q Data to longer arrays
%Expands the original data by copying each number a few times
function [I_mod Q_mod] = expandIQ(I,Q,expandfactor)
expandfactor = round(expandfactor);
I_mod = [];
Q_mod = [];
for i=1:expandfactor:(length(I)*expandfactor)
I_mod = [I_mod I((i-1)/expandfactor+1)*ones(1,expandfactor)];
Q_mod = [Q_mod Q((i-1)/expandfactor+1)*ones(1,expandfactor)];
end
if(length(I_mod) ~= length(I)*expandfactor)
error('Unexpected length of I_mod')
end
I have tried 'pre-allocating for speed' as matlab suggested, however this reduced it drastically.
I have been testing the performance using:
I = ones(1,length_of_I);
Q = ones(1,length_of_I);
tic
expandIQ(I,Q,expandfactor);
toc
where length_of_I << expandfactor
eg. length_of_I = 16, expandfactor = 500000 or length_of_I = 500, expandfactor = 50000
Im sure there is a faster way but i can't think of any. I am more comfortable in C than matlab so any suggestions/ideas before i try mex?
Thanks, Stephen

Best Answer

Hi Stephen,
See if the following brings an improvement in your specific context:
function [I_mod Q_mod] = expandIQ(I,Q,expandfactor)
expandfactor = round(expandfactor) ;
I_mod = reshape(repmat(I, expandfactor, 1), 1, []) ;
Q_mod = reshape(repmat(Q, expandfactor, 1), 1, []) ;
end
It's not too relevant here, but there is a profiler in the Tools menu.
Cheers,
Cedric