MATLAB - Correction based on multiple condition. Help to speed up a function
up vote
0
down vote
favorite
I need help to speed up a function I created to correct x
and y
elements based on the position of obstacles.
I explain better my function with one example only in the x
direction (this means 1D example). Let's suppose I have the values of x
and Geometry
as
x = [1.8;3.4;4.2;6.3;6.4;8.6;9.1];
Geometry = [0;1;1;0;1;0;1;1;0;0]; % the value 1 means obstacle
So if I do find(Geometry)
i'll get the position of the obstacles. Then the function corrects the position based on the x
elements that are greater than the position of the obstacles. So for example x(1)
is not corrected because its position is not greater than any obstacle. But x(2)
will be corrected by two Geometry
elements so Corrected_x(2) = 3.4 - 2
. The answer of this example is:
Corrected_x = [1.8;1.4;2.2;3.3;3.4;3.6;4.1];
Note that the last element of x
is corrected by 5 because the value is greater than the position of the 5 obstacles present in Geometry
.
So far I have written a function that does this in 2D and gives me the result I want but it takes too much time. The problem is that Geometry is MxN
matrix that I need to go through every row and column since the obstacle are in different positions in each row or column.
The function takes too much time because of the loops going through every row and column, but also because x
and y
are large arrays.
Any ideas to speed this up?
function [Corrected_x, Corrected_y]= PositionCorrection(x, y, Geometry)
yRange = floor(min(y)):ceil(max(y));
xRange = floor(min(x)):ceil(max(x));
nY = length(yRange); % nRows
nX = length(xRange); % nCols
%% Save in cell arrays the x and y elements within the ranges
xPositionCorrected = cell(nY-1,1);
yPositionCorrected = cell(nX-1,1);
% Remember the order of the elements
Rememberpositionx = cell(nY-1,1);
Rememberpositiony = cell(nX-1,1);
%% Loop of search and correction of x elements
for iY = 1:nY-1
% x elements within yRange
xinBinY = x(y(:)>yRange(iY) & y(:)<yRange(iY+1));
[~,xObstacle] = find(Geometry(iY,:)); % xPosition of the obstacle
[~,Rememberpositionx{iY,1}] = ismember(xinBinY,x);
% Correction
if isempty(xinBinY)
xPositionCorrected{iY,1} = ;
elseif isempty(xObstacle)
xPositionCorrected{iY,1} = xinBinY;
else
matrixCorrectedResults = bsxfun(@gt, xinBinY,xObstacle);
xCorrection = sum(matrixCorrectedResults,2);
xPositionCorrected{iY,1} = xinBinY-xCorrection;
end
end
%% Loop of search and correction of y elements
for iX = 1:nX-1
% y elements within xRange
yinBinX = y(x(:)>xRange(iX) & x(:)<xRange(iX+1));
[yObstacle,~] = find(Geometry(:,iX));
[~,Rememberpositiony{iX,1}] = ismember(yinBinX,y);
if isempty(yinBinX)
yPositionCorrected{iX,1} = ;
elseif isempty(yObstacle)
yPositionCorrected{iX,1} = yinBinX;
else
matrixCorrectedResults = bsxfun(@gt, yinBinX,yObstacle.');
yCorrection = sum(matrixCorrectedResults,2);
yPositionCorrected{iX,1} = yinBinX-yCorrection;
end
end
%% Get the original order
xPositionCorrected(all(cellfun('isempty', xPositionCorrected),2),:) = ;
Rememberpositionx(all(cellfun('isempty',Rememberpositionx),2),:) = ;
yPositionCorrected(all(cellfun('isempty', yPositionCorrected),2),:) = ;
Rememberpositiony(all(cellfun('isempty',Rememberpositiony),2),:) = ;
% x
Lx = 1:length(x);
Corrected_x = cell2mat(xPositionCorrected);
idx = cell2mat(Rememberpositionx);
[~,idtemp] = ismember(Lx,idx);
Corrected_x = Corrected_x(idtemp);
% y
Corrected_y = cell2mat(yPositionCorrected);
idy = cell2mat(Rememberpositiony);
[~,idtemp] = ismember(Lx,idy);
Corrected_y = Corrected_y(idtemp);
end
arrays matlab performance function for-loop
add a comment |
up vote
0
down vote
favorite
I need help to speed up a function I created to correct x
and y
elements based on the position of obstacles.
I explain better my function with one example only in the x
direction (this means 1D example). Let's suppose I have the values of x
and Geometry
as
x = [1.8;3.4;4.2;6.3;6.4;8.6;9.1];
Geometry = [0;1;1;0;1;0;1;1;0;0]; % the value 1 means obstacle
So if I do find(Geometry)
i'll get the position of the obstacles. Then the function corrects the position based on the x
elements that are greater than the position of the obstacles. So for example x(1)
is not corrected because its position is not greater than any obstacle. But x(2)
will be corrected by two Geometry
elements so Corrected_x(2) = 3.4 - 2
. The answer of this example is:
Corrected_x = [1.8;1.4;2.2;3.3;3.4;3.6;4.1];
Note that the last element of x
is corrected by 5 because the value is greater than the position of the 5 obstacles present in Geometry
.
So far I have written a function that does this in 2D and gives me the result I want but it takes too much time. The problem is that Geometry is MxN
matrix that I need to go through every row and column since the obstacle are in different positions in each row or column.
The function takes too much time because of the loops going through every row and column, but also because x
and y
are large arrays.
Any ideas to speed this up?
function [Corrected_x, Corrected_y]= PositionCorrection(x, y, Geometry)
yRange = floor(min(y)):ceil(max(y));
xRange = floor(min(x)):ceil(max(x));
nY = length(yRange); % nRows
nX = length(xRange); % nCols
%% Save in cell arrays the x and y elements within the ranges
xPositionCorrected = cell(nY-1,1);
yPositionCorrected = cell(nX-1,1);
% Remember the order of the elements
Rememberpositionx = cell(nY-1,1);
Rememberpositiony = cell(nX-1,1);
%% Loop of search and correction of x elements
for iY = 1:nY-1
% x elements within yRange
xinBinY = x(y(:)>yRange(iY) & y(:)<yRange(iY+1));
[~,xObstacle] = find(Geometry(iY,:)); % xPosition of the obstacle
[~,Rememberpositionx{iY,1}] = ismember(xinBinY,x);
% Correction
if isempty(xinBinY)
xPositionCorrected{iY,1} = ;
elseif isempty(xObstacle)
xPositionCorrected{iY,1} = xinBinY;
else
matrixCorrectedResults = bsxfun(@gt, xinBinY,xObstacle);
xCorrection = sum(matrixCorrectedResults,2);
xPositionCorrected{iY,1} = xinBinY-xCorrection;
end
end
%% Loop of search and correction of y elements
for iX = 1:nX-1
% y elements within xRange
yinBinX = y(x(:)>xRange(iX) & x(:)<xRange(iX+1));
[yObstacle,~] = find(Geometry(:,iX));
[~,Rememberpositiony{iX,1}] = ismember(yinBinX,y);
if isempty(yinBinX)
yPositionCorrected{iX,1} = ;
elseif isempty(yObstacle)
yPositionCorrected{iX,1} = yinBinX;
else
matrixCorrectedResults = bsxfun(@gt, yinBinX,yObstacle.');
yCorrection = sum(matrixCorrectedResults,2);
yPositionCorrected{iX,1} = yinBinX-yCorrection;
end
end
%% Get the original order
xPositionCorrected(all(cellfun('isempty', xPositionCorrected),2),:) = ;
Rememberpositionx(all(cellfun('isempty',Rememberpositionx),2),:) = ;
yPositionCorrected(all(cellfun('isempty', yPositionCorrected),2),:) = ;
Rememberpositiony(all(cellfun('isempty',Rememberpositiony),2),:) = ;
% x
Lx = 1:length(x);
Corrected_x = cell2mat(xPositionCorrected);
idx = cell2mat(Rememberpositionx);
[~,idtemp] = ismember(Lx,idx);
Corrected_x = Corrected_x(idtemp);
% y
Corrected_y = cell2mat(yPositionCorrected);
idy = cell2mat(Rememberpositiony);
[~,idtemp] = ismember(Lx,idy);
Corrected_y = Corrected_y(idtemp);
end
arrays matlab performance function for-loop
Indenting your code will format it properly, rather than adding backticks before and after every single line!
– Wolfie
Nov 19 at 12:28
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need help to speed up a function I created to correct x
and y
elements based on the position of obstacles.
I explain better my function with one example only in the x
direction (this means 1D example). Let's suppose I have the values of x
and Geometry
as
x = [1.8;3.4;4.2;6.3;6.4;8.6;9.1];
Geometry = [0;1;1;0;1;0;1;1;0;0]; % the value 1 means obstacle
So if I do find(Geometry)
i'll get the position of the obstacles. Then the function corrects the position based on the x
elements that are greater than the position of the obstacles. So for example x(1)
is not corrected because its position is not greater than any obstacle. But x(2)
will be corrected by two Geometry
elements so Corrected_x(2) = 3.4 - 2
. The answer of this example is:
Corrected_x = [1.8;1.4;2.2;3.3;3.4;3.6;4.1];
Note that the last element of x
is corrected by 5 because the value is greater than the position of the 5 obstacles present in Geometry
.
So far I have written a function that does this in 2D and gives me the result I want but it takes too much time. The problem is that Geometry is MxN
matrix that I need to go through every row and column since the obstacle are in different positions in each row or column.
The function takes too much time because of the loops going through every row and column, but also because x
and y
are large arrays.
Any ideas to speed this up?
function [Corrected_x, Corrected_y]= PositionCorrection(x, y, Geometry)
yRange = floor(min(y)):ceil(max(y));
xRange = floor(min(x)):ceil(max(x));
nY = length(yRange); % nRows
nX = length(xRange); % nCols
%% Save in cell arrays the x and y elements within the ranges
xPositionCorrected = cell(nY-1,1);
yPositionCorrected = cell(nX-1,1);
% Remember the order of the elements
Rememberpositionx = cell(nY-1,1);
Rememberpositiony = cell(nX-1,1);
%% Loop of search and correction of x elements
for iY = 1:nY-1
% x elements within yRange
xinBinY = x(y(:)>yRange(iY) & y(:)<yRange(iY+1));
[~,xObstacle] = find(Geometry(iY,:)); % xPosition of the obstacle
[~,Rememberpositionx{iY,1}] = ismember(xinBinY,x);
% Correction
if isempty(xinBinY)
xPositionCorrected{iY,1} = ;
elseif isempty(xObstacle)
xPositionCorrected{iY,1} = xinBinY;
else
matrixCorrectedResults = bsxfun(@gt, xinBinY,xObstacle);
xCorrection = sum(matrixCorrectedResults,2);
xPositionCorrected{iY,1} = xinBinY-xCorrection;
end
end
%% Loop of search and correction of y elements
for iX = 1:nX-1
% y elements within xRange
yinBinX = y(x(:)>xRange(iX) & x(:)<xRange(iX+1));
[yObstacle,~] = find(Geometry(:,iX));
[~,Rememberpositiony{iX,1}] = ismember(yinBinX,y);
if isempty(yinBinX)
yPositionCorrected{iX,1} = ;
elseif isempty(yObstacle)
yPositionCorrected{iX,1} = yinBinX;
else
matrixCorrectedResults = bsxfun(@gt, yinBinX,yObstacle.');
yCorrection = sum(matrixCorrectedResults,2);
yPositionCorrected{iX,1} = yinBinX-yCorrection;
end
end
%% Get the original order
xPositionCorrected(all(cellfun('isempty', xPositionCorrected),2),:) = ;
Rememberpositionx(all(cellfun('isempty',Rememberpositionx),2),:) = ;
yPositionCorrected(all(cellfun('isempty', yPositionCorrected),2),:) = ;
Rememberpositiony(all(cellfun('isempty',Rememberpositiony),2),:) = ;
% x
Lx = 1:length(x);
Corrected_x = cell2mat(xPositionCorrected);
idx = cell2mat(Rememberpositionx);
[~,idtemp] = ismember(Lx,idx);
Corrected_x = Corrected_x(idtemp);
% y
Corrected_y = cell2mat(yPositionCorrected);
idy = cell2mat(Rememberpositiony);
[~,idtemp] = ismember(Lx,idy);
Corrected_y = Corrected_y(idtemp);
end
arrays matlab performance function for-loop
I need help to speed up a function I created to correct x
and y
elements based on the position of obstacles.
I explain better my function with one example only in the x
direction (this means 1D example). Let's suppose I have the values of x
and Geometry
as
x = [1.8;3.4;4.2;6.3;6.4;8.6;9.1];
Geometry = [0;1;1;0;1;0;1;1;0;0]; % the value 1 means obstacle
So if I do find(Geometry)
i'll get the position of the obstacles. Then the function corrects the position based on the x
elements that are greater than the position of the obstacles. So for example x(1)
is not corrected because its position is not greater than any obstacle. But x(2)
will be corrected by two Geometry
elements so Corrected_x(2) = 3.4 - 2
. The answer of this example is:
Corrected_x = [1.8;1.4;2.2;3.3;3.4;3.6;4.1];
Note that the last element of x
is corrected by 5 because the value is greater than the position of the 5 obstacles present in Geometry
.
So far I have written a function that does this in 2D and gives me the result I want but it takes too much time. The problem is that Geometry is MxN
matrix that I need to go through every row and column since the obstacle are in different positions in each row or column.
The function takes too much time because of the loops going through every row and column, but also because x
and y
are large arrays.
Any ideas to speed this up?
function [Corrected_x, Corrected_y]= PositionCorrection(x, y, Geometry)
yRange = floor(min(y)):ceil(max(y));
xRange = floor(min(x)):ceil(max(x));
nY = length(yRange); % nRows
nX = length(xRange); % nCols
%% Save in cell arrays the x and y elements within the ranges
xPositionCorrected = cell(nY-1,1);
yPositionCorrected = cell(nX-1,1);
% Remember the order of the elements
Rememberpositionx = cell(nY-1,1);
Rememberpositiony = cell(nX-1,1);
%% Loop of search and correction of x elements
for iY = 1:nY-1
% x elements within yRange
xinBinY = x(y(:)>yRange(iY) & y(:)<yRange(iY+1));
[~,xObstacle] = find(Geometry(iY,:)); % xPosition of the obstacle
[~,Rememberpositionx{iY,1}] = ismember(xinBinY,x);
% Correction
if isempty(xinBinY)
xPositionCorrected{iY,1} = ;
elseif isempty(xObstacle)
xPositionCorrected{iY,1} = xinBinY;
else
matrixCorrectedResults = bsxfun(@gt, xinBinY,xObstacle);
xCorrection = sum(matrixCorrectedResults,2);
xPositionCorrected{iY,1} = xinBinY-xCorrection;
end
end
%% Loop of search and correction of y elements
for iX = 1:nX-1
% y elements within xRange
yinBinX = y(x(:)>xRange(iX) & x(:)<xRange(iX+1));
[yObstacle,~] = find(Geometry(:,iX));
[~,Rememberpositiony{iX,1}] = ismember(yinBinX,y);
if isempty(yinBinX)
yPositionCorrected{iX,1} = ;
elseif isempty(yObstacle)
yPositionCorrected{iX,1} = yinBinX;
else
matrixCorrectedResults = bsxfun(@gt, yinBinX,yObstacle.');
yCorrection = sum(matrixCorrectedResults,2);
yPositionCorrected{iX,1} = yinBinX-yCorrection;
end
end
%% Get the original order
xPositionCorrected(all(cellfun('isempty', xPositionCorrected),2),:) = ;
Rememberpositionx(all(cellfun('isempty',Rememberpositionx),2),:) = ;
yPositionCorrected(all(cellfun('isempty', yPositionCorrected),2),:) = ;
Rememberpositiony(all(cellfun('isempty',Rememberpositiony),2),:) = ;
% x
Lx = 1:length(x);
Corrected_x = cell2mat(xPositionCorrected);
idx = cell2mat(Rememberpositionx);
[~,idtemp] = ismember(Lx,idx);
Corrected_x = Corrected_x(idtemp);
% y
Corrected_y = cell2mat(yPositionCorrected);
idy = cell2mat(Rememberpositiony);
[~,idtemp] = ismember(Lx,idy);
Corrected_y = Corrected_y(idtemp);
end
arrays matlab performance function for-loop
arrays matlab performance function for-loop
edited Nov 19 at 12:50
rinkert
1,047316
1,047316
asked Nov 19 at 12:20
S. Perez
176
176
Indenting your code will format it properly, rather than adding backticks before and after every single line!
– Wolfie
Nov 19 at 12:28
add a comment |
Indenting your code will format it properly, rather than adding backticks before and after every single line!
– Wolfie
Nov 19 at 12:28
Indenting your code will format it properly, rather than adding backticks before and after every single line!
– Wolfie
Nov 19 at 12:28
Indenting your code will format it properly, rather than adding backticks before and after every single line!
– Wolfie
Nov 19 at 12:28
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374505%2fmatlab-correction-based-on-multiple-condition-help-to-speed-up-a-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Indenting your code will format it properly, rather than adding backticks before and after every single line!
– Wolfie
Nov 19 at 12:28