Linearly spaced vectors

조회 수: 7 (최근 30일)
Ragaar Ashnod
Ragaar Ashnod 2011년 3월 2일
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에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by