how to split an array with a periodic set of elements as it reaches to the maximum elements?

3 ビュー (過去 30 日間)
lets say i have an array with periodic set of elements such as below:
A=[2 2 2 3 3 4 4 5 5 5 1 1 1 2 2 2 2 3 3 4 4 4 4 5 5 1 1 2 3 3 4 4 4 5 5];
i want to split Array A in to 3 separate arrays(in this case) as it reach to element 5 as below:
B1=[2 2 2 3 3 4 4 5 5 5];
B2=[1 1 1 2 2 2 2 3 3 4 4 4 4 5 5];
B3=[1 1 2 3 3 4 4 4 5 5];
i greatly appreciate your help
thank you

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 5 月 24 日
B = mat2cell(A(:)',1,diff(find([true,diff([A(:)'== 5,0]) == -1])));
  3 件のコメント
Joseph
Joseph 2017 年 5 月 24 日
thank you for answer, however when i apply to my array, it gives me below error:
Input arguments, D1 through D2, must sum to each dimension of the input matrix size, [1 32025]
i should mention that my array size is A(32025,1);
Andrei Bobrov
Andrei Bobrov 2017 年 5 月 25 日
編集済み: Andrei Bobrov 2017 年 5 月 25 日
Please attach your array A with size [32025 x 1] as mat-file.

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

その他の回答 (1 件)

KSSV
KSSV 2017 年 5 月 24 日
A=[2 2 2 3 3 4 4 5 5 5 1 1 1 2 2 2 2 3 3 4 4 4 4 5 5 1 1 2 3 3 4 4 4 5 5];
didx = diff(A==5) ;
idx = find(didx==-1) ;
idx = [0 idx length(A)] ;
N = length(idx)-1 ;
iwant = cell(N,1) ;
for i = 1:N
iwant{i} = A(idx(i)+1:idx(i+1)) ;
end
celldisp(iwant)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by