Repmat a vector n times where n is not a whole number

조회 수: 7 (최근 30일)
Stef
Stef 2018년 6월 24일
편집: Ankita Bansal 2018년 6월 25일
I have a vector y consisting out of 10 elements. I want have a for loop, where I repmat the vector y to a new vector. However repmat is not working since n is not always a whole number. In this case I want it to choose the element which is next, e.g. if n = 4.2 it should take y 4 times and add element 1 and 2 separately. How can I do this?
y = [1:10];
for n = 4:0.5:6
Z = repmat(y,n,1);
end
  댓글 수: 3
Stephen23
Stephen23 2018년 6월 25일
편집: Stephen23 2018년 6월 25일
Adam
Adam 2018년 6월 25일
Also there were a couple of answers to essentially the same question here:

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 6월 25일
편집: Stephen23 2018년 6월 25일
To take the smaller index (equivalent to fix):
>> y = 1:10;
>> n = 4.2;
>> [repmat(y,1,fix(n)),y(1:numel(y)*mod(n,1))]
ans = 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2
  댓글 수: 5
Stephen23
Stephen23 2018년 6월 25일
편집: Stephen23 2018년 6월 25일
"could you please show me how to make the 12 times 3 matrix instead of a 4 times 9? I mean for a matrix of four rows that row 5 is the same as row 1"
I don't understand. My example shows a 4x3 matrix and n=2.5, which would give a 10x3 matrix. How do you expect to get a 12x3 matrix from that?
>> [repmat(y,fix(n),1);y(1:size(y,1)*mod(n,1),:)]
ans =
2 9 4
5 2 3
6 4 9
5 4 7
2 9 4
5 2 3
6 4 9
5 4 7
2 9 4
5 2 3
Stef
Stef 2018년 6월 25일
Thank you very much, Stephen!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ankita Bansal
Ankita Bansal 2018년 6월 25일
편집: Ankita Bansal 2018년 6월 25일
Hi Stef, are you saying that you have a 4x9 matrix A and you want to reshape it to 12x3? If so then you can use reshape function available in MATLAB.
But if you are saying that you want to create a 12x3 matrix B, where B(5,:) is equal to B(1,:) and B(6,:) is equal to B(2,:) and so on, then which elements from A do you want to drop?
Or are you saying that you want to create a 12x9 matrix from A? if so then also you can write
M= [repmat(y,fix(n),1);y(1:size(y,1)*mod(n,1),:)]

카테고리

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