Best way to reduce to a simple loop?

1 次查看(过去 30 天)
Oscar Zampi
Oscar Zampi 2021-2-20
回答: Jan 2021-2-21
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 个评论
Adam Danz
Adam Danz 2021-2-20
😨
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.

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2021-2-21
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by