필터 지우기
필터 지우기

Systematically add entries to vector array

조회 수: 3 (최근 30일)
MiauMiau
MiauMiau 2017년 8월 22일
편집: Stephen23 2017년 8월 22일
Hi,
Given I have a vector as for instance: [ 1 3 5 7 1 3 ...]
I want to add twice the number "8" after each entry, hence we would have:
[ 1 8 8 3 8 8 5 8 8 7 8 8 1 8 8 3 8 8 ...]
How could I do that, in a way which would be not specific for the length of my initial array? Thanks

채택된 답변

Stephen23
Stephen23 2017년 8월 22일
편집: Stephen23 2017년 8월 22일
Here is one efficient method, without creating any temporary variables:
>> V = [1 3 5 7 1 3];
>> V(2:3,:) = 8;
>> V(:).'
ans =
1 8 8 3 8 8 5 8 8 7 8 8 1 8 8 3 8 8

추가 답변 (1개)

KSSV
KSSV 2017년 8월 22일
A = [ 1 3 5 7 1 3] ;
N = (length(A))*2+length(A) ;
C = 8*ones(N,1) ;
C(1:3:end) = A ;
  댓글 수: 1
José-Luis
José-Luis 2017년 8월 22일
N = (length(A))*2+length(A) ;
What's wrong with length(A) .* 3?
:)

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by