Replace every nth element in a vector
조회 수: 67 (최근 30일)
이전 댓글 표시
Hey guys,
I struggle with a rather simple question. I would like to generate a zero-vector with the length of the projectlength and then replace every xth value (which equals the lifetime) with the costs.
projectlength = 50;
costs = 30;
lifetime = 10;
Inv = zeros(projectlength,1);
Inv(0:lifetime:projectlength)= costs;
I do not seem to get it right (Error message: Array indices must be positive integers or logical values.) although it is quite simple, I guess. Thanks for helping out!
댓글 수: 0
채택된 답변
Timo Dietz
2021년 2월 1일
You must not start your index with '0' but with '1'
Inv(1:lifetime:projectlength)= costs;
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!