Repeat copies of array elements using for loop/while
이전 댓글 표시
Hi, I need create a code where the elements of an array are repeat:
For example:
% Create a simple vector:
for i =1:4
Vec(i) = i;
end
Vec = [1,2,3,4];
If I need repeat the elements of this vector 2 times:
Vec = [1,1,2,2,3,3,4,4];
3 times:
Vec = [1,1,1,2,2,2,3,3,3,4,4,4];
I know using the function: repelem; I can get this result but I need do it using a for loop/while.
¿Any idea?
댓글 수: 6
Image Analyst
2022년 2월 26일
Why? You didn't tag it as homework so since it's not your homework why won't you use repelem()?
Juan Pérez Álvarez
2022년 2월 26일
Image Analyst
2022년 2월 27일
OK, if you want to create your own, then I'll let you do it. I'll just give a hint if you want it. The index "i" in the loop should be a vector of length 2 that specifies a starting and stopping index, like instead of 2 it should be [3:4].
Juan Pérez Álvarez
2022년 2월 27일
Image Analyst
2022년 2월 27일
What if you subtract 1 from i before multiplying it by 2?
Juan Pérez Álvarez
2022년 2월 27일
편집: Image Analyst
2022년 2월 27일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!