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

5 次查看(过去 30 天)
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
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

请先登录,再进行评论。

更多回答(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 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by