Error with find function in a for loop

1 回表示 (過去 30 日間)
Kafayat Olayinka
Kafayat Olayinka 2020 年 2 月 6 日
コメント済み: TADA 2020 年 2 月 6 日
Time, height, and temperature variable all have the same array size. I want find temperature at altitude greater than 6km. I also want 6kmtemp to be the same size as temperature. However, after running this code, the result was an empty array. t3 is 0*0. How can I make this find function work in a for loop. BTW, the find function worked when i didnt have it in the loop. However, the array size changed. What am i missing? Thanks
for i=1:length(Time)
t3=find(height(i)>=6);
6kmtemp(i,t3)=temperature(t3);
end
  2 件のコメント
Star Strider
Star Strider 2020 年 2 月 6 日
Note that:
6kmtemp
is not an acceptable variable name, and will throw an error.
Consider:
temp6km
instead.
Kafayat Olayinka
Kafayat Olayinka 2020 年 2 月 6 日
That is true. however, that was not the error. After running that code, t3 array is empty. meaning the find function in the loop didn't work.

サインインしてコメントする。

採用された回答

TADA
TADA 2020 年 2 月 6 日
Find accepts a logical vector and returns all the indices containing 1.
The problem is you are giving find a scalar value so it's going to return an empty value when height(i) < 6 and 1 whenever height(i) >= 6
Did you consider not using find at all? Instead you can switch to logical indexing:
6kmtemp = nan(size(temperature));
atLeast6km = height >= 6;
6kmtemp(atLeast6km) = temperature(atLeast6km);
You should preallocate 6kmtemp to whatever value you want the "empty" cells to have, I used NaN but you can define your own constants according to what makes sense for you
  4 件のコメント
Kafayat Olayinka
Kafayat Olayinka 2020 年 2 月 6 日
Thank you. It worked!
TADA
TADA 2020 年 2 月 6 日
Cheers

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by