필터 지우기
필터 지우기

For loop with two array

조회 수: 14 (최근 30일)
Jacqueline Rigatto
Jacqueline Rigatto 2021년 5월 21일
댓글: Jacqueline Rigatto 2021년 5월 23일
I have two line matrices and I would like to make a loop comparing each line of the two matrices and making a third line matrix (with a formula by comparing u and v). My code is looping infinitely.
x=load('wind_uv.txt');
[numRows,numCols] = size(x);
u_10=x(:,1);
v_10=x(:,2);
% direction
for u = 1:numRows
for v = 1:numRows
if u>0
Dir(u,v) = atan(v/u);
elseif v>=0 & u<0
Dir(u,v) = (atan(v/u))+pi;
elseif v<0 & u<0
Dir(u,v) = (atan(v/u))-pi;
elseif v>0 & u==0
Dir(u,v) = +pi/2;
elseif v<0 & u==0
Dir(u,v) = -pi/2;
else
Dir(u,v) = 'undefined';
end
end
end
Dir;
  댓글 수: 3
Jacqueline Rigatto
Jacqueline Rigatto 2021년 5월 22일
I posted a part of the file in txt
DGM
DGM 2021년 5월 22일
편집: DGM 2021년 5월 22일
That's good, but you haven't addressed the comments below. The code does not loop infinitely, it's just extremely inefficient. For the small sample data, the calculated array is 210x210, which only takes about 10ms to calculate. For a dataset with 2000 lines, this balloons to about 10 seconds -- a 1000x more time for only 10x more lines.
We could offer a more efficient way of doing the calculations, but it's not clear what you're trying to calculate at all. The entire conditional structure is redundant.
for u = 1:numRows
for v = 1:numRows
if u>0
% this condition is never false
% this is the only line that will ever run
Dir(u,v) = atan(v/u);
elseif v>=0 & u<0
% u is never negative
Dir(u,v) = (atan(v/u))+pi;
elseif v<0 & u<0
% neither condition is ever true
Dir(u,v) = (atan(v/u))-pi;
elseif v>0 & u==0
% u is never zero
Dir(u,v) = +pi/2;
elseif v<0 & u==0
% neither condition is ever true
Dir(u,v) = -pi/2;
else
% this is going to cause an error
% 'undefined' is a character vector
% Dir(u,v) is a numeric scalar
Dir(u,v) = 'undefined';
end
end
end
All of this code can be reduced to two lines, about 500x faster:
x = 1:numRows;
Dir2 = atan(x./x.');
What's confusing is that none of the data is actually used for anything.

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2021년 5월 23일
Perhaps you're trying to calculate wind direction and trying to conditionally deal with the fact that atan() has a limited domain. If that's the case, use atan2(), which is the 4-quadrant arctangent.
Dir2 = atan2(u_10,v_10);
plot(Dir2)
That's my best guess at the moment
  댓글 수: 1
Jacqueline Rigatto
Jacqueline Rigatto 2021년 5월 23일
Thank you for the help, @DGM. That was it!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jaya
Jaya 2021년 5월 22일
I don't know about the infinite looping reason but one thing I can ask is, are you sure the code is written as you intended? Because the u and v in the for loop are from 1: numRows and so the if u>0 condition will always be true. I think first you may have to check if it is on some other variable/parameter that you need find atan for. Like I don't see where you are again using the u_10,v_10 and intensity...
  댓글 수: 1
DGM
DGM 2021년 5월 22일
Also, it's worth pointing out the fact that u,v both span 1:numRows, despite one being a column index, leaving numCols unused. Similarly, the data in x is never used for anything; all of the operations in the loops are functions of the indices themselves and nothing else.

댓글을 달려면 로그인하십시오.

카테고리

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