필터 지우기
필터 지우기

Split variable length vector by parts

조회 수: 2 (최근 30일)
YT
YT 2018년 4월 25일
편집: YT 2018년 4월 25일
So it's probably an easy solution, but can't really find the answer that quick.
I have the following vector:
V = [1 2 3 4 5 ... n];
P = ceil(length(V/127)); %dont know if ceil or floor is better
And then I want to split the vector `V` in parts `P`, so that I'll get something like the following:
% as an example n = 2184 & P = 17
C = {[1 2 3 ... 18],[18 19 20 ... 35],...[2167 2168 2169 ...2184]};
Thanks in advance
  댓글 수: 1
Ameer Hamza
Ameer Hamza 2018년 4월 25일
편집: Ameer Hamza 2018년 4월 25일
You mentioned that P=17 but your first element of C contain 18 elements. What to do, if elements of V can't be divided properly, e.g. 2184/17 is not an integer. How do you want to handle it?

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

채택된 답변

dpb
dpb 2018년 4월 25일
L-length(V);
P=17;
id2=[repmat(P,1,fix(L/P)) rem(L,P)]; % number elements in cell size P matching L plus remainder
C=mat2cell(V,1,id2);
That's for the nearest multiple of P with whatever is left over tacked onto the end irregardless of its size relative to P. If it's needed to make as near even as possible need more logic to choose the bin size; that's not a given criterion in the question posed, however.
  댓글 수: 1
YT
YT 2018년 4월 25일
편집: YT 2018년 4월 25일
While it works fine and I accepted your answer, I've created a new question related to this one (for more logical sizes of subvectors) >here<

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by