How to pick group sum values from an array

3 ビュー (過去 30 日間)
Poorna Durga Geesupalli
Poorna Durga Geesupalli 2022 年 11 月 28 日
コメント済み: William Rose 2022 年 12 月 2 日
How can I do the group sum in an array.
X=[1111100000000010000000100011100011111];
In this X array, I wanna group the values where index 1 occus for 3 or more consecutive cells.
Answer like X1=sum(5+1+1+3+5)=15;
But I need X2=groupsum[5 3 5];
How can I do this.
Thanks,
  1 件のコメント
Stephen23
Stephen23 2022 年 12 月 2 日
X = '1111100000000010000000100011100011111';
[S,E] = regexp(X,'1{3,}');
N = E-S+1
N = 1×3
5 3 5

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

採用された回答

William Rose
William Rose 2022 年 11 月 28 日
編集済み: William Rose 2022 年 11 月 28 日
[edit: correct a typo in one of the comment lines in the code, fix indenting, add comments]
Xs='1111100000000010000000100011100011111';
%% Convert string of digits to array of numbers
X=zeros(1,length(Xs));
for i=1:length(X),X(i)=str2num(Xs(i)); end
%% Process the array, looking for groups of 3 or more consecutive 1's
i=3; % i=current position in vector X
k=1; % k=current position in vector X2
X2=[];
while i<=length(X)
j=i-2;
c=1; %c=1 indicates we should check X1
while c && i<=length(X)
X1=sum(X(j:i));
if X1<(i-j+1)
c=0; %There is at least one "0" in the group, so move one
end
i=i+1;
end
if X1>=3
X2=[X2,X1]; %append X1 to X2
k=k+1;
end
end
%% Display result
fprintf('X2 =');disp(X2)
X2 = 5 3 5
Try it. Good luck.
  4 件のコメント
Stephen23
Stephen23 2022 年 12 月 2 日
編集済み: Stephen23 2022 年 12 月 2 日
Calling STR2NUM inside a loop in order to convert from char to numeric is very inefficient.
Two simple MATLAB approaches:
X = '1111100000000010000000100011100011111';
X-'0'
ans = 1×37
1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0
sscanf(X,'%1u',[1,Inf])
ans = 1×37
1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0
William Rose
William Rose 2022 年 12 月 2 日
THank you @Stephen23. Those are nice to know about.

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

その他の回答 (2 件)

chrisw23
chrisw23 2022 年 11 月 28 日
編集済み: chrisw23 2022 年 11 月 28 日
X=[1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1];
xStr = string(X).join("");
pat = asManyOfPattern("1",3); % group of at least 3 times "1"
matchVec = xStr.extract(pat);
count = matchVec.join.strlength;
...a step by step example using string functions for this special case

Vilém Frynta
Vilém Frynta 2022 年 11 月 28 日
I tried this: (converting into text, deleting zeros, then finding strings of ones and checking their length)
but I have to go. I might come back later to finish this, but you get the idea. Hope I helped.
x = [1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,1,1,1,1,1];
xTxt = num2str(x)
xTxt = '1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1'
a = strsplit(xTxt,"0")
a = 1×23 cell array
{'1 1 1 1 1 '} {' '} {' '} {' '} {' '} {' '} {' '} {' '} {' '} {' 1 '} {' '} {' '} {' '} {' '} {' '} {' '} {' 1 '} {' '} {' '} {' 1 1 1 '} {' '} {' '} {' 1 1 1 1 1'}

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by