Repmat a vector n times where n is not a whole number
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
Réponse acceptée
Stephen23
le 25 Juin 2018
Modifié(e) : Stephen23
le 25 Juin 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 commentaires
Stephen23
le 25 Juin 2018
Modifié(e) : Stephen23
le 25 Juin 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
Plus de réponses (1)
Ankita Bansal
le 25 Juin 2018
Modifié(e) : Ankita Bansal
le 25 Juin 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),:)]
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!