How to repeat an array based on another array
3 次查看(过去 30 天)
显示 更早的评论
Greetings
I am trying to repeat an array based on another array e.g
Repeat element of array A based on the elements of array B
A= [ 1 2 3 4 ]
B= [ 2 3 1 2]
the resulted array should be ( first element of A that is 1 repeated 2 times which is the first element of array B and so on )
C= [ 1 1 2 2 2 3 4 4 ]
0 个评论
采纳的回答
更多回答(1 个)
Stephen23
2017-1-15
For MATLAB before R2015a:
>> A = [1,2,3,4];
>> B = [2,3,1,2];
>> cell2mat(arrayfun(@(a,b)repmat(a,1,b),A,B,'uni',0))
ans =
1 1 2 2 2 3 4 4
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!