# if or for loop

1 次查看（过去 30 天）

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 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 个评论显示隐藏 1更早的评论
Image Analyst 2021-10-13

### Community Treasure Hunt

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

Start Hunting!

Translated by