필터 지우기
필터 지우기

Repeat array onto the same row

조회 수: 2 (최근 30일)
Leonardo
Leonardo 2023년 9월 9일
댓글: Leonardo 2023년 9월 9일
I want A=[1,2,3,4] to
become A=[1,2,3,4,1,2,3,4] by x2
or
become A=[1,2,3,4,1,2] by x1.5
is it possible?

채택된 답변

Paul
Paul 2023년 9월 9일
A=[1,2,3,4]
A = 1×4
1 2 3 4
horzcat(A,A)
ans = 1×8
1 2 3 4 1 2 3 4
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
ans = 1×6
1 2 3 4 1 2

추가 답변 (1개)

Dyuman Joshi
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 = 1×8
1 2 3 4 1 2 3 4
B = repetition(A,1.5)
B = 1×6
1 2 3 4 1 2
B = repetition(A,2.75)
B = 1×11
1 2 3 4 1 2 3 4 1 2 3
B = repetition(A,3.3)
Error using solution>repetition
Incompatible number for repetition
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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by