MATLAB - Correction based on multiple condition. Help to speed up a function












0














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









share|improve this question
























  • Indenting your code will format it properly, rather than adding backticks before and after every single line!
    – Wolfie
    Nov 19 '18 at 12:28
















0














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









share|improve this question
























  • Indenting your code will format it properly, rather than adding backticks before and after every single line!
    – Wolfie
    Nov 19 '18 at 12:28














0












0








0







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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 12:50









rinkert

1,377417




1,377417










asked Nov 19 '18 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 '18 at 12:28


















  • Indenting your code will format it properly, rather than adding backticks before and after every single line!
    – Wolfie
    Nov 19 '18 at 12:28
















Indenting your code will format it properly, rather than adding backticks before and after every single line!
– Wolfie
Nov 19 '18 at 12:28




Indenting your code will format it properly, rather than adding backticks before and after every single line!
– Wolfie
Nov 19 '18 at 12:28

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory