当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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(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

分享给朋友:
您可能感兴趣的文章:
随机阅读: