Azzera filtri
Azzera filtri

For loop with two array

14 visualizzazioni (ultimi 30 giorni)
Jacqueline Rigatto
Jacqueline Rigatto il 21 Mag 2021
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 Commenti
Jacqueline Rigatto
Jacqueline Rigatto il 22 Mag 2021
I posted a part of the file in txt
DGM
DGM il 22 Mag 2021
Modificato: DGM il 22 Mag 2021
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.

Accedi per commentare.

Risposta accettata

DGM
DGM il 23 Mag 2021
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

Più risposte (1)

Jaya
Jaya il 22 Mag 2021
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 Commento
DGM
DGM il 22 Mag 2021
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.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by