For loop with two array
显示 更早的评论
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 个评论
DGM
2021-5-22
I don't see how the loop could become infinite here. It might help to attach the wind_uv.txt file or provide a format-alike simulant so that the code can be tested. Perhaps it simply requires a very long execution time due to the dataset size and the per-element looping approach.
Jacqueline Rigatto
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.
采纳的回答
更多回答(1 个)
Jaya
2021-5-22
0 个投票
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
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!