Linearly spaced vectors

2 次查看(过去 30 天)
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

采纳的回答

Ragaar Ashnod
Ragaar Ashnod 2011-3-2
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

更多回答(3 个)

the cyclist
the cyclist 2011-3-2
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 个评论
Ragaar Ashnod
Ragaar Ashnod 2011-3-2
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.

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2011-3-2
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 个评论
Ragaar Ashnod
Ragaar Ashnod 2011-3-2
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 2011-3-2
Just change the function handle:
y = cellfun(@(x)[x(1), ceil(x(1)):increment:floor(x(2)), x(2)],x2,'uni',false)

请先登录,再进行评论。


Oleg Komarov
Oleg Komarov 2011-3-2
Have you seen mcolon, could be a good benchmark.
  1 个评论
Ragaar Ashnod
Ragaar Ashnod 2011-3-2
Sadly, I had never heard of it before. Thanks for the link!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by