Create a vector with block elements of the same value

조회 수: 8 (최근 30일)
Martijn
Martijn 2012년 3월 9일
For my superfast code (no element wise evaluation of collocation points, but vector-wise) to evaluate Uniform cubic B-splines, I am looking for an answer to the following question:
I wish to create a vector that should contain this N blocks of size k the same values (starting from 1,2,3...up to N). As an example:
suppose k=3 and N=3, I want: [1 1 1 2 2 2 3 3 3 ]
I should be quite an easy thing to do, but I just can figure it out how to do it. Hope you guys can help!
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 3월 9일
ndgrid() or meshgrid(), take a single output and reshape it to be a vector.
I do not have access at the moment to test the exact sequence.

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

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 3월 9일
K=3;
N=3;
v = reshape(repmat(1:K,N,1),1,N*K)

추가 답변 (1개)

Oleg Komarov
Oleg Komarov 2012년 3월 9일
Another approach, run-length decoding:
nk = n*k;
out = zeros(1,nk);
out(1:k:nk) = 1;
out = cumsum(out);

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by