Loop for this program

I'm writing a program for an assignment and it involves taking coordinates and using the haversine formula to find the distance between them. I've done that but now I need to be able to find all the times that the distance (d) falls below 27m. I believe I need to use a loop to increment the variable count every time the distance does fall below 27m and use that for my fprintf statement but not what type of loop to use nor do I know what to enter next to the loop. Such as: while [statement]

回答 (1 件)

OCDER
OCDER 2017 年 10 月 2 日
編集済み: OCDER 2017 年 10 月 2 日

0 投票

Looks like d is a vector. You can use a logical array ( d < 27 ) to find where in d is under 27, and then use this logical array with time to get the time d is under 27.
undertime = time( d < 27 )
To print all the times d is under 27 m, you can simply do this:
fprintf('d is under 27 at: %d\n', undertime)
No for loop needed, unless you want to output results one by one.

2 件のコメント

Abu Jalloh
Abu Jalloh 2017 年 10 月 2 日
I need a loop so that it increments count by 1 every time d < 27 so that I can print one line fprintf('d is under 27 %d times', count); instead of multiple lines that would be output from your code
OCDER
OCDER 2017 年 10 月 2 日
Here's the for loop version.
underidx = find(d < 27);
for count = 1:length(underidx)
fprintf('d is under 27 at: %d\n, time(underidx(count)));
end

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

タグが未入力です。

質問済み:

2017 年 10 月 2 日

編集済み:

2017 年 10 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by