필터 지우기
필터 지우기

Split the long vector into shorter ones

조회 수: 6 (최근 30일)
Daria Ivanchenko
Daria Ivanchenko 2020년 5월 20일
댓글: Daria Ivanchenko 2020년 5월 21일
Hi!
I have a long vector that I want to split into the shorter ones. The vector contains only ones and zeros. I would like to split the vector from 1 to the next 1 (and not including this 1). For example, I have a 17x1 vector A = [1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0] . I want to split it into smaller ones. So in the end I would like to have: В = [1 0 0 0], С = [1 0 0 0 0], D = [1 0 0 0 0 0], E = [1 0]. How can I do it?
Thanks a lot fot any help!

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 20일
편집: Ameer Hamza 2020년 5월 20일
Try this
A = [1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0];
groups = cumsum(A);
B = splitapply(@(x) {x}, A, groups);
It creates a cell array with split arrays. This link show why you should avoid naming variables B, C, D, .. : https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
  댓글 수: 5
Ameer Hamza
Ameer Hamza 2020년 5월 20일
Following will work if A is the matrix with last column 0 and 1.
groups = cumsum(A(:,end));
B = splitapply(@(x) {x}, A, groups);
Daria Ivanchenko
Daria Ivanchenko 2020년 5월 21일
Yes, it works! Thank you so much!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by