Best way to reduce to a simple loop?

1 view (last 30 days)
Oscar Zampi
Oscar Zampi on 20 Feb 2021
Answered: Jan on 21 Feb 2021
Hi, I have the following section of code that I would like to simplify down as much as possible so that it can do the same thing, but am unsure how to do so.
Any help would be greatly apopreciated!
if A(1,3)<A(2,3) && A(1,3)<A(3,3) && A(1,3)<A(4,3) && A(1,3)<A(5,3)
E1i = A(1,1);
E1j = A(1,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(1,:) = [];
else if A(2,3)<A(1,3) && A(2,3)<A(3,3) && A(2,3)<A(4,3) && A(2,3)<A(5,3)
E1i = A(2,1);
E1j = A(2,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(2,:) = [];
else if A(3,3)<A(1,3) && A(3,3)<A(2,3) && A(3,3)<A(4,3) && A(3,3)<A(5,3)
E1i = A(3,1);
E1j = A(3,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(3,:) = [];
else if A(4,3)<A(1,3) && A(4,3)<A(2,3) && A(4,3)<A(3,3) && A(4,3)<A(5,3)
E1i = A(4,1);
E1j = A(4,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(4,:) = [];
else A(5,3)<A(1,3) && A(5,3)<A(2,3) && A(5,3)<A(3,3) && A(5,3)<A(4,3)
E1i = A(5,1);
E1j = A(5,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(5,:) = [];
end
end
end
end
if A(1,3)<A(2,3) && A(1,3)<A(3,3) && A(1,3)<A(4,3)
E1i = A(1,1);
E1j = A(1,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(1,:) = [];
else if A(2,3)<A(1,3) && A(2,3)<A(3,3) && A(2,3)<A(4,3)
E1i = A(2,1);
E1j = A(2,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(2,:) = [];
else if A(3,3)<A(1,3) && A(3,3)<A(2,3) && A(3,3)<A(4,3)
E1i = A(3,1);
E1j = A(3,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(3,:) = [];
else if A(4,3)<A(1,3) && A(4,3)<A(2,3) && A(4,3)<A(3,3)
E1i = A(4,1);
E1j = A(4,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(4,:) = [];
end
end
end
end
if A(1,3)<A(2,3) && A(1,3)<A(3,3)
E1i = A(1,1);
E1j = A(1,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(1,:) = [];
else if A(2,3)<A(1,3) && A(2,3)<A(3,3)
E1i = A(2,1);
E1j = A(2,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(2,:) = [];
else if A(3,3)<A(1,3) && A(3,3)<A(2,3)
E1i = A(3,1);
E1j = A(3,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(3,:) = [];
end
end
end
  1 Comment
Adam Danz
Adam Danz on 20 Feb 2021
😨
Before the loops are optimized, there are some serious problems to address with the existing code.
  1. The indentation make is very difficult to understand how these conditions are nested. From the Matlab editor, select all of the text (ctrl+a) and then smart-indent (ctrl+i).
  2. Look at all of the orange tick marks along the right side of the editor. They indicate warnings. Not all warnings are serious but the ones listed here are serious.
  3. "else if" is must different from "elseif". Please review the documentation: doc elseif
  4. Edit your question to clean up the code so that it represents what you want it to do and fix the problems listed above.

Sign in to comment.

Answers (1)

Jan
Jan on 21 Feb 2021
The first block can be simplified to:
i2 = 2; % This is 2 in the first iteration, and 1 afterwards
index = NaN;
for i1 = 1:5
if A(i1,3)<A(i2,3) && A(i1,3)<A(3,3) && A(i1,3)<A(4,3) && A(i1,3)<A(5,3)
index = i1:
break;
end
i2 = 1;
end
E1i = A(index,1);
E1j = A(index,2);
E1y = 913000 - E1i;
E1x = E1j + 207000;
plot(E1x, E1y,'rx')
A(index, :) = [];
Avoid repeated code by moving it outside the loop.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!