Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
matlab programming issues

Subject: matlab programming issues

From: Marie

Date: 20 Jan, 2009 01:39:01

Message: 1 of 3

We are having the same issues using your suggested map method. I think it has something to do with the way we are using the for loops. Instead, we changed our if statements in order to have a 1 to 1 comparison so we know that wont be an issue. We have also tried simplifying this type of code as much as possible and are still having issues. This small amount of code is used to remove rows from the neighbor list that are already in the closed list. Keep in mind, our closed list and neighbor list consist of [i,j,C] coordinates where C is the cost of that node.

When we run this code we end up getting many extra and duplicate rows in 'comparisonlist2' even tho we do match the first if statement and MatLab will display "im on the closed list..." We have tried running matlab's unique command on the resulting 'comparisonlist2' but it ends up being equal to the original list.

Do you have any suggestions?

%rowscomparison = # of rows in neighbor list
%rowsclosed = # of rows in closed list

comparisonlist2 = [];
for j = 1:rowscomparison
     for i = 1:rowsclosed
        if closedlist(i,1) == comparisonlist(j,1) && closedlist(i,2) == comparisonlist(j,2)
            display('Im on the closed list already')
        else
             comparisonlist2 = [comparisonlist2;comparisonlist(j,:)];
        end
     end
end

Subject: matlab programming issues

From: someone

Date: 20 Jan, 2009 03:30:05

Message: 2 of 3

"Marie " <mek482@drexel.edu> wrote in message <gl39vl$bcp$1@fred.mathworks.com>...
> We are having the same issues using your suggested map method. I think it has something to do with the way we are using the for loops. Instead, we changed our if statements in order to have a 1 to 1 comparison so we know that wont be an issue. We have also tried simplifying this type of code as much as possible and are still having issues. This small amount of code is used to remove rows from the neighbor list that are already in the closed list. Keep in mind, our closed list and neighbor list consist of [i,j,C] coordinates where C is the cost of that node.
>
> When we run this code we end up getting many extra and duplicate rows in 'comparisonlist2' even tho we do match the first if statement and MatLab will display "im on the closed list..." We have tried running matlab's unique command on the resulting 'comparisonlist2' but it ends up being equal to the original list.
>
> Do you have any suggestions?
>
> %rowscomparison = # of rows in neighbor list
> %rowsclosed = # of rows in closed list
>
> comparisonlist2 = [];
> for j = 1:rowscomparison
> for i = 1:rowsclosed
> if closedlist(i,1) == comparisonlist(j,1) && closedlist(i,2) == comparisonlist(j,2)
> display('Im on the closed list already')
> else
> comparisonlist2 = [comparisonlist2;comparisonlist(j,:)];
> end
> end
> end

% Its not a good idea to compare two floats for equality.
% See Q6.1 of the MATLAB FAQ at:

http://matlabwiki.mathworks.com/MATLAB_FAQ

% Instead of:

if a == b

% Try something like:

if abs(a-b) < tol

% where tol is some tolerance limit you are OK with.

Subject: matlab programming issues

From: Roger Stafford

Date: 20 Jan, 2009 03:34:02

Message: 3 of 3

"Marie " <mek482@drexel.edu> wrote in message <gl39vl$bcp$1@fred.mathworks.com>...
> We are having the same issues using your suggested map method. I think it has something to do with the way we are using the for loops. Instead, we changed our if statements in order to have a 1 to 1 comparison so we know that wont be an issue. We have also tried simplifying this type of code as much as possible and are still having issues. This small amount of code is used to remove rows from the neighbor list that are already in the closed list. Keep in mind, our closed list and neighbor list consist of [i,j,C] coordinates where C is the cost of that node.
>
> When we run this code we end up getting many extra and duplicate rows in 'comparisonlist2' even tho we do match the first if statement and MatLab will display "im on the closed list..." We have tried running matlab's unique command on the resulting 'comparisonlist2' but it ends up being equal to the original list.
>
> Do you have any suggestions?
>
> %rowscomparison = # of rows in neighbor list
> %rowsclosed = # of rows in closed list
>
> comparisonlist2 = [];
> for j = 1:rowscomparison
> for i = 1:rowsclosed
> if closedlist(i,1) == comparisonlist(j,1) && closedlist(i,2) == comparisonlist(j,2)
> display('Im on the closed list already')
> else
> comparisonlist2 = [comparisonlist2;comparisonlist(j,:)];
> end
> end
> end

  The logic is faulty here. The way you have it, any time you find an i-th pair of closedlist which differs from a j-th pair of comparisonlist, you stick the comparisonlist pair into comparisonlist2. You are bound to get numerous duuplications in comparisonlist2 that way. You need a comparison between comparisonlist and comparisonlist2 to avoid such duplication.

  However, doing it with these loops is terribly inefficient. I recommend you use the 'ismember' function with the 'rows' option. Its efficiency is due to a sorting action which is done to avoid unnecessary numbers of comparisons.

 tf = ismember(comparisonlist,closedlist,'rows');
 comparisonlist(tf) = [];

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us