Linearly spaced vectors

For academic interest, I wish to further improve the speed and memory performance of the included code. The function's purpose, which I've provided below, is to generate a series of linearly spaced vectors using only an input vector for defining the upper and lower limits.
EXAMPLE:
x = [0
12.872
21.453
30.132
40.371
57.631
77.816
100];
increment = 2
y = f(x,increment)
An important aspect to note is that, with the exception of edge conditions, the generated outputs are even and monotonically increasing:
y = [0, ..., 12, 12.872,
12.872, 14, ..., 20, 21.453,
21.453, 22, ..., 30, 30.132,
...
57.631, 58, ..., 76, 77.816,
77.816, 78, ..., 100];
The method currently being used is as follows:
function y = multi_linspace(x,increment)
% Multi_linspace generates a vector of linearily spaced vectors.
input_length = length(x);
fractional = @(x)([fix(x)*Inf^(x-fix(x)),x]);
for i=1:input_length
if (i ~= input_length)
buffer{i}=ceil(x(i))+mod(ceil(x(i)),2):increment:x(i+1);
edge(1,:)=fractional(x(i));
edge(2,:)=fractional(x(i+1));
id = isinf(edge(:,1));
prefix = id(1); suffix = id(2);
if prefix
% x(i) == edge(1,2)
buffer{i} = [x(i),buffer{i}];
end
if suffix
% x(i+1) == edge(2,2)
buffer{i} = [buffer{i},x(i+1)];
end
end
end
y=[buffer{:}]';
end

 Respuesta aceptada

Ragaar Ashnod
Ragaar Ashnod el 2 de Mzo. de 2011

0 votos

The speed of a single call execution is on average better with the original code. However, it is [clearly] difficult to read and difficult for matlab's internal process(es) to optimize for subsequent calls. Inspired by, and barely improving upon, the cyclist's code the following code provides significant improvement for subsequent calls. And sort is likely faster only because it is a built-in function.
function y = multi_linspace2(x,increment)
y = (x(1):increment:x(end))';
y = sort([y;x(2:end-1);x(2:end-1)]);
end

Más respuestas (3)

the cyclist
the cyclist el 2 de Mzo. de 2011

1 voto

I made no attempt to understand the complexity of your code, but I did check that the following reproduces your result exactly, for the test case you gave.
I don't know if it's faster, but it is shorter and simpler. But maybe you are testing special cases, which I am not.
function y = multi_linspace2(x,increment)
y = (x(1):increment:x(end))';
for ii=2:length(x)-1
y = [y(y<x(ii)); x(ii); x(ii); y(y>=x(ii))];
end
end

1 comentario

Ragaar Ashnod
Ragaar Ashnod el 2 de Mzo. de 2011
Was actually showing pretty good results with mine, but like you said it is really complex.
Your post helped inspire a potential follow-up, though.

Iniciar sesión para comentar.

Sean de Wolski
Sean de Wolski el 2 de Mzo. de 2011

0 votos

x2 = [x(1:end-1), x(2:end)]
x2 = num2cell(x2,2)
y = cellfun(@(x)x(1):increment:x(2),x2,'uni',false)
%Scd

2 comentarios

Ragaar Ashnod
Ragaar Ashnod el 2 de Mzo. de 2011
This was the first path I attempted. The values generated progress the decimal places included and that is something I needed to avoid.
Sean de Wolski
Sean de Wolski el 2 de Mzo. de 2011
Just change the function handle:
y = cellfun(@(x)[x(1), ceil(x(1)):increment:floor(x(2)), x(2)],x2,'uni',false)

Iniciar sesión para comentar.

Oleg Komarov
Oleg Komarov el 2 de Mzo. de 2011

0 votos

Have you seen mcolon, could be a good benchmark.

1 comentario

Ragaar Ashnod
Ragaar Ashnod el 2 de Mzo. de 2011
Sadly, I had never heard of it before. Thanks for the link!

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by