简体   繁体   中英

In MATLAB, how to find indexes in vector every time value change from 1 to 0?

I have a vector of 1s and 0s that represent when an intermittent data signal is occurring. Eg:

    V = [0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,0];

How do I find the index value of every change from 1 to 0? So for vector V above, the answer would be:

    idx = [10,18,28];

Quick and easy:

idx=find(diff(V)<0)+1;

Compute the derivative, get only the negative values (from 1 to 0 is -1) and find the locations. As the derivatives start from the second location, we need to add 1

However, note that if what you want is accessing data on those locations, it's better to use the logical indices directly, as in:

somevals=my_other_data([false;diff(V)<0]);

Read more about general variable indexing here

strfind不仅仅是字符串,也适合像你这样的情况。

idx = strfind(V,[1 0]) + 1;

你也可以使用卷积:

idx = find(conv(V,[-1,1])==1);

If you want a generic solution which works for "index of every time the value changes from x to y ", use this vectorised approach:

idx = find( ( V(1:end-1) == x ) & V(2:end) == y ) ) + 1;

In your case

idx = find( ( V(1:end-1) == 1 ) & ( V(2:end) == 0 ) ) + 1;
% >> idx = [10 18 28]

As with Ander's solution, if you're using this for indexing then find is an unnecessary slow-down, just use this

idx = [false, ( V(1:end-1) == 1 ) & ( V(2:end) == 0 )];

You can use find to locate 0's and 1's and use intersect to find edges, in general

>> intersect(find(V==0), find(V==1)+1)
ans =
    10    18    28

You can replace 0 and 1 with any arbitrary value. But if you are dealing with only 0's and 1's you can simplify it to

>> intersect(find(~V), find(V)+1)
ans =
    10    18    28

One way would be to write a loop. It's been a while since I've used matlab so forgive me if my syntax is a bit off, but something with similar logic to this should work:

Given your vector V:

idx = [];
for i = 1:(length(V) - 1)
  if (V(i) == 0 && V(i+1) == 1)
    idx = [idx, (i + 1)];
  end
end

Good luck! Let me know if it doesn't work for some reason. If you're going to be performing this actions a lot, you should probably write a function too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM