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

6 visualizaciones (últimos 30 días)
Stef
Stef el 24 de Jun. de 2018
Editada: Ankita Bansal el 25 de Jun. de 2018
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 comentarios
Stephen23
Stephen23 el 25 de Jun. de 2018
Editada: Stephen23 el 25 de Jun. de 2018
Adam
Adam el 25 de Jun. de 2018
Also there were a couple of answers to essentially the same question here:

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 25 de Jun. de 2018
Editada: Stephen23 el 25 de Jun. de 2018
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 comentarios
Stephen23
Stephen23 el 25 de Jun. de 2018
Editada: Stephen23 el 25 de Jun. de 2018
"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 el 25 de Jun. de 2018
Thank you very much, Stephen!

Iniciar sesión para comentar.

Más respuestas (1)

Ankita Bansal
Ankita Bansal el 25 de Jun. de 2018
Editada: Ankita Bansal el 25 de Jun. de 2018
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),:)]

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by