필터 지우기
필터 지우기

How to split an array at a specific component?

조회 수: 1 (최근 30일)
Ferial
Ferial 2021년 2월 14일
댓글: Ferial 2021년 2월 14일
I have an array of length x (lets say 10) and I want to split the array at every occurence of the value 0.
For example A = [0,3,5,6,9,0,2], I want to form two separate array from this being B=[3,5,6,9] and C=[2]
Taking into consideration that the 0 could be anywhere in the array, could be in any number of occurences and could even not exist at all.

채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 14일
A = [0,3,5,6,9,0,2]
A = 1×7
0 3 5 6 9 0 2
mask = A == 0;
starts = strfind([true mask], [1 0]);
stops = strfind([mask true], [0 1]);
output = arrayfun(@(T,P) A(T:P), starts, stops, 'uniform', 0);
celldisp(output)
output{1} = 3 5 6 9 output{2} = 2

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by