Calculate true in binary array specifically

3 次查看(过去 30 天)
Hello!
I have a boolean one-dimensional array, like
binary = [0,0,0,1,0,1,0,1,1,1,0,1,1,1,0,0,0,1,1,0 ..., 0]
And I like to squeeze from this the following:
[1, 1, 3, 3, 2, ...]
So, I would like to calculate how many 1s (Trues) encounter in row. It is possible to write a loop somehow, like:
for i=1:size(array)
if B(14:i)== 1
count = count + 1;
else:
count = 0;
Result_Array.append(count) % I do not know builtin function exaclty.
end
Is it correct..? And I would like to know, if there is a standart matlab function for this purpose.
Thank you very much!

采纳的回答

Stephen23
Stephen23 2018-9-23
编辑:Stephen23 2018-9-23
This is MATLAB, so you don't need to use loops to solve every little task:
>> V = [0,0,0,1,0,1,0,1,1,1,0,1,1,1,0,0,0,1,1,0,0];
>> D = diff([0,V,0]);
>> find(D<0)-find(D>0)
ans =
1 1 3 3 2

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by