Split a vector

조회 수: 21 (최근 30일)
fcarl
fcarl 2011년 9월 21일
Hi,
I have a question regarding vectors: I have a vector of size n and I want to split the vector by a matlab function so that I get elements 1-10, 2-11, 3-12....
Is there any built-in-function?
Thanks!

채택된 답변

Grzegorz Knor
Grzegorz Knor 2011년 9월 21일
Split to one matrix:
n = 20;
% create vector a
a = 1:n;
m = cell2mat(arrayfun(@(x)a(x:x+9)',1:n-9,'UniformOutput',false));
Splitting to one matrix you can also perform with this code:
a = rand(20,1)
n = length(a);
[x y] = meshgrid(0:n-10,1:10);
a(x+y)
Split to vectors:
n = 20;
% create vector a
a = 1:n;
for k=1:n-9
eval(['vec' num2str(k) ' = a(k:k+9);'])
end
  댓글 수: 1
Jan
Jan 2011년 9월 21일
Do you know the BUFFER command?

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

추가 답변 (1개)

fcarl
fcarl 2011년 9월 21일
I think I did not describe my problem exactly. My aim is to vectorize for example this code:
x=10;
vec=1:1000;
result=[];
for i=1:end-x
result[i]=vec(i:i+x);
end;
  댓글 수: 1
Grzegorz Knor
Grzegorz Knor 2011년 9월 21일
In my opinion I've given the solutions in the post above :)
x=10;
vec=1:1000;
[i1 i2] = meshgrid(0:x-1,1:length(vec)-x+1);
result = vec(i1+i2)

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by