How to repeat each elements of matrix irregularly
3 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (1 件)
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
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.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!