How to repeat each elements of matrix irregularly

조회 수: 2 (최근 30일)
Jenjen Ahmad Zaeni
Jenjen Ahmad Zaeni 2021년 5월 27일
댓글: Jenjen Ahmad Zaeni 2021년 5월 28일
Hello everyone, i have a matrix like:
A =
0.2978 -0.7649 -2.8475 3.4084
And i want to repeat the elements of matrix but differently for each element. For example, i want to repeat 0.2978 two times, -0.7649 three times, -2.8475 two times, and 3.4084 four times. So it will look like:
B =
0.2978 0.2978 -0.7649 -0.7649 -0.7649 -2.8475 -2.8475 3.4084 3.4084 3.4084 3.4084
What is the way to do that? Thank you.

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 5월 27일
Hi,
This is an easy solution to your exercise. You can adjust it w.r.t your exercise conditions:
A =[0.2978 -0.7649 -2.8475 3.4084];
AA=[];
for ii = 2:numel(A)
AA = [AA, repmat(A(ii-1), 1, ii)];
end
Good luck.
  댓글 수: 2
Jenjen Ahmad Zaeni
Jenjen Ahmad Zaeni 2021년 5월 28일
Works for me. Thank you very much.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 5월 28일
Most welcome!!!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 5월 27일
Use repelem():
A =[0.2978 -0.7649 -2.8475 3.4084]
B = repelem(A, [2 : length(A) + 1])
  댓글 수: 3
Image Analyst
Image Analyst 2021년 5월 28일
편집: Image Analyst 2021년 5월 28일
Wow, about time to upgrade. There has been so many improvements in the last 7 years as you can imagine. You can edit the release even after you post you know. I've done it for you this time.
Jenjen Ahmad Zaeni
Jenjen Ahmad Zaeni 2021년 5월 28일
Haha yeah. So many improvement that make things easier. When i installed it, i have less memory of HDD so i installed the older release instead. Oh yes, my bad, i didn't look for it. Anyway, thank you very much.

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

카테고리

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

제품


릴리스

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by