开发者

MATLAB: Finding the entry number of the first '1' in a logical array

开发者 https://www.devze.com 2023-03-25 22:10 出处:网络
I have created a logical array of 1\'s and 0\'s using the following code: nWindow = 10; LowerTotInitial = std(LowerTot(1:nWindow));

I have created a logical array of 1's and 0's using the following code:

nWindow = 10;
LowerTotInitial = std(LowerTot(1:nWindow));
UpperTotInitial = std(UpperTot(1:nWindow));
flag = 0;
flagArray = zeros开发者_StackOverflow(length(LowerTot), 1);
for n = 1 : nData0 - nWindow
    for k = 0 : nWindow - 1 
         if LowerTot(n + k) < 0.1*LowerTotInitial || UpperTot(n + k) < 0.1*UpperTotInitial
             flag = 1;
             flagArray(n) = 1;
         else
             flag = 0;
         end
    end 
end

This returns flagArray, an array of 0's and 1's. I am trying to find the index of the first 1 in the array. ie. 1 = flagArray(index). I am confused as to what is the best way to accomplish this!


What you call an entry number is referred to as an index in MATLAB-speak. To find the index of the first matching element in an array you can use the FIND function:

>> x = [0 0 1 0 1 0];
>> find(x, 1, 'first')

ans =

     3


Try this ind = find(flagArray, k, 'first') with k =1

Read this Matlab Docs - find

0

精彩评论

暂无评论...
验证码 换一张
取 消