perform calculation using for loop
조회 수: 5 (최근 30일)
이전 댓글 표시
Adam Kevin Francis Baker
2019년 2월 17일
댓글: Walter Roberson
2019년 2월 18일
So I have to use a for loop because I'm a student and the teacher said so. I've been trying to figure this one out and I'm sure theres a simple solution but because we are using matlab there aren't a whole lot of tutorials on using for loops when array operations are so much better.
I need to calculate the specific humidity using a for loop.
The equation is: q = (E*e)/P
where E = 0.622, e is a 96x144 matrix and, P represents p1 in my code which is another 96x144 matrix
I've coded something but I feel like the for loop is too simple.
Also the for loop and array operation return different values...is this normal?
% use for loop to calculate specific humidity
for q = 1:numel(p1)
q_for = (0.622.*e)./(p1(q));
end
% calculate specific humidity using array operations
q_array = (0.622.*e)./p1;
댓글 수: 2
Walter Roberson
2019년 2월 17일
Your output in your for loop should probably be going into a variable indexed at something.
채택된 답변
추가 답변 (1개)
Adam Kevin Francis Baker
2019년 2월 18일
댓글 수: 2
Walter Roberson
2019년 2월 18일
That would have been okay under one of two circumstances:
- You pre-initialized q_for to be size(p1); or
- You reshaped vector q_for to be size(p1)
참고 항목
카테고리
Help Center 및 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!