Repeat array onto the same row
1 次查看(过去 30 天)
显示 更早的评论
采纳的回答
Paul
2023-9-9
A=[1,2,3,4]
horzcat(A,A)
horzcat(A,A(1:floor(numel(A)/2))) % or use ceils depending on how you want to handle the case where numel(A) is odd
更多回答(1 个)
Dyuman Joshi
2023-9-9
编辑:Dyuman Joshi
2023-9-9
Here's a generalised code -
A=[1,2,3,4];
B = repetition(A,2)
B = repetition(A,1.5)
B = repetition(A,2.75)
B = repetition(A,3.3)
function B = repetition(A,n)
B = repmat(A,1,ceil(n));
k = numel(A)*n;
%k needs to be an integer to repeat an array by a non-integer number
%Check for a number being an integer is the reminder when divided by 1 is zero
if rem(k,1)
error('Incompatible number for repetition');
else
B = B(1:k);
end
end
另请参阅
类别
在 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!