For loop with two array

36 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by