if or for loop

2 ビュー (過去 30 日間)
Arif Hoq
Arif Hoq 2021 年 10 月 13 日
コメント済み: Image Analyst 2021 年 10 月 13 日
Hello,
I am confused about this issue. i have an excel file with 2 colomn. first column is the time(hours) and second column is the power. like:
time= 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
power= 0,0,0,0,0,12,40,100,200,300,500,800,700,650,600,500,400,200,0,0,0,0,0,0
Now, I want to implement,
First: whenever the value of 'power' is greater than 0, then it will show the time (i.e 6)
Last: whenever the value of 'power' is greater than 0, then it will show the time (i.e 19)
Please give your suggestion. I have attachded the excel file. Thank you very much.

採用された回答

Dave B
Dave B 2021 年 10 月 13 日
You can find the index of the first and last instance of power greater than 0 using the find function, then put those indices right into time to get the values:
time= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
power= [0,0,0,0,0,12,40,100,200,300,500,800,700,650,600,500,400,200,0,0,0,0,0,0];
time(find(power>0,1,'first'))
ans = 6
time(find(power>0,1,'last'))
ans = 18
  2 件のコメント
Arif Hoq
Arif Hoq 2021 年 10 月 13 日
It's working. Thanks a lot Dave.
Image Analyst
Image Analyst 2021 年 10 月 13 日
Then please "Accept this answer". Thanks in advance.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by