# Help to Improve this short code (create a rect given initial point, final point and slope)

1 visualización (últimos 30 días)
Aaron Garcia el 30 de Mayo de 2020
Comentada: Aaron Garcia el 30 de Mayo de 2020
Hello there!!
I have write this code to find the Y values of a line, given two points and slope (calculate previous call the funtion), its to simple but in implementation i calling this function around 10 million times, so i need te reduce the time that it takes, if someone have some advice it would be helpful thank you!!!.
function[y] = LineFuntion(pts,m,Pi,Pf)
y=zeros(1,pts);
if abs(m) == Inf % vertical line
y(1:pts) = linspace(Pi(2),Pf(2),pts);
elseif m == 0 % horizontal line
y(1:pts) = Pi(2);
else
x=zeros(1,pts);
x(1:pts) = linspace(Pi(1),Pf(1),pts); %this line is the most time consuming
y(1:pts) = m*(x-Pi(1))+Pi(2); %this line is the second most time consuming
end
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Mehmed Saad el 30 de Mayo de 2020
function[y] = LineFuntion(pts,m,Pi,Pf)
if abs(m) == Inf % vertical line
y = linspace(Pi(2),Pf(2),pts);
elseif m == 0 % horizontal line
y = repmat(Pi(2),1,pts);
else
x = linspace(Pi(1),Pf(1),pts);
y = m*(x-Pi(1))+Pi(2);
end
end
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Aaron Garcia el 30 de Mayo de 2020
Thanks it work better!, let me wait if some one comment something else , so i can acept your answer.

Iniciar sesión para comentar.

### Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by