function to pick out number

2 次查看(过去 30 天)
August edwards
August edwards 2011-12-8
Okay I have a 400000x2 matrix named A with data of velocity 2nd row versus time 1st row. Now each of these datas are a lot of 0's for the velocity and then suddenly there will be like 50 elements that are nonzero and this pattern continues. Now I want to define a function that can give me the first and last non-zero velocity in such a sequence. I've tried with the if-function but don't really know what to write. I wrote something like: for i=1:400000 if A(i,2)>0 A(i,2)=b end but first of all this would give me(if it worked) all the velocities in the sequence and I only want the 1st and last. Secondly I don't think you can use i as a variable in that sense..

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2011-12-8
Use this example, your second column data will be the variable "A" below.
A=[0 0 1 2 3 4 5 0 0 0 4 5 6 7 8 0 0];
TF=A~=0;
Ind=diff(TF);
StartValue=A(find(Ind==1)+1)
EndValue=A(find(Ind==-1))

标签

Community Treasure Hunt

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

Start Hunting!

Translated by