How to count elements in array until criteria is met and output number of elements?

7 ビュー (過去 30 日間)
Nicholas Kavouris
Nicholas Kavouris 2022 年 3 月 28 日
コメント済み: Matt J 2022 年 3 月 29 日
I have an array documenting states of a system and need to find system active duration.
An array looks something like
[1 1 1 1 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 1 1 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5]
Each integer is the system state output from the controller. How can I create a method to count the number of elements in the array until the array becomes 5 (shutdown), repeat counting for the next system cycle, and output the duration (number of elements) of each cycle independently?

回答 (2 件)

Image Analyst
Image Analyst 2022 年 3 月 28 日
Try using regionprops(), if you have the Image Processing Toolbox (type ver to check)
v = [1 1 1 1 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 1 1 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5];
v5 = v == 5;
% Find starting locations and durations
props = regionprops(v5, 'PixelList', 'Area');
for k = 1 : length(props)
startingLocations(k) = props(k).PixelList(1,1);
end
startingLocations % Echo to command window
startingLocations = 1×2
33 58
durations = [props.Area] % How many 5's are in each run of 5's.
durations = 1×2
4 3
  4 件のコメント
Nicholas Kavouris
Nicholas Kavouris 2022 年 3 月 28 日
i do not, appreciate the help though
Image Analyst
Image Analyst 2022 年 3 月 28 日
Well that gets you the number of non-5 counts though.
By the way, take a look at splitapply(), groupsummary(), and findgroups(). They are handy functions to know about.

サインインしてコメントする。


Matt J
Matt J 2022 年 3 月 28 日
編集済み: Matt J 2022 年 3 月 28 日
Download this,
Then,
array = [1 1 1 1 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 1 1 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5];
G=groupTrue(array~=5);
[~,~,durations]=groupLims(G,1)
durations =
32 21
  2 件のコメント
Nicholas Kavouris
Nicholas Kavouris 2022 年 3 月 29 日
Can this function be used for multiple conditons? Also reading your examples does not include the groupLims fcn
Matt J
Matt J 2022 年 3 月 29 日
Can this function be used for multiple conditons?
You mean like this?
G=groupTrue(array~=5 & array~=2);
The argument to groupTrue can be any logical vector.
Also reading your examples does not include the groupLims fcn
Now you have one!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by