divide a vector in subvectors

조회 수: 9 (최근 30일)
ale ale
ale ale 2016년 5월 10일
답변: Guillaume 2016년 5월 10일
Hi, I have a long vector, and I want to divide it for example in subvectors of size 100. without using a reshape() because I don't know the size of my long vector and so it doesn't fit perfectly in a M*N matrix. So, how can I do it? thank you
  댓글 수: 1
KSSV
KSSV 2016년 5월 10일
편집: KSSV 2016년 5월 10일
why you don't know the size of vector? Once you have it in MATLAB, you can get its size using size(vector)?

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

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2016년 5월 10일
편집: Andrei Bobrov 2016년 5월 10일
A = randi(12,27,1); % - your vector
M = 4;
out = reshape([A;nan(mod(-numel(A),M),1)],M,[]);

Guillaume
Guillaume 2016년 5월 10일
You can split your vector into a cell array of subvectors using mat2cell:
v = rand(1, 1024); %demo data. length is not a multiple of 100
splitlength = 100;
sublengths = ones(1, ceil(numel(v)/splitlength)) * splitlength;
sublengths(end) = sublengths(end) + numel(v) - sum(sublengths); %adjust length of last block
subvectors = mat2cell(v, 1, sublengths)

카테고리

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