how to create an array from a given array with the following criteria.
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Pranjal Kaura
le 12 Juin 2020
Réponse apportée : Ameer Hamza
le 12 Juin 2020
Say given array is [1 2 3]. I want to create an array with the elements [1 2 3 4 5 2 3 4 5 6 3 4 5 6 7],
In others terms I want to create an array [1:1+4 2:2+4 3:3+4]
I do not want to use for loops as that slows my code considerably.
0 commentaires
Réponse acceptée
madhan ravi
le 12 Juin 2020
a = 1:3
Wanted = reshape(cumsum([a; ones(4,numel(a))]),1,[])
0 commentaires
Plus de réponses (1)
Ameer Hamza
le 12 Juin 2020
An alternative solution using automatic array expansion
a = 1:3;
b = reshape(a + (0:4)', [], 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!