# How to repeat each elements of matrix irregularly

1 view (last 30 days)
Jenjen Ahmad Zaeni on 27 May 2021
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 on 27 May 2021
Hi,
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 CommentsShowHide 1 older comment
Sulaymon Eshkabilov on 28 May 2021
Most welcome!!!

Image Analyst on 27 May 2021
Use repelem():
A =[0.2978 -0.7649 -2.8475 3.4084]
B = repelem(A, [2 : length(A) + 1])
Jenjen Ahmad Zaeni on 28 May 2021
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.

R2014b

### Community Treasure Hunt

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

Start Hunting!

Translated by