What's the best way to create a vector with a repeating and diminutive sequence?

1 vue (au cours des 30 derniers jours)
For a small example, how to create a version of this, with a longer seequence. These are integers of equal length apart:
v = 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 ....
and the starting sequence length will be scaled up into the 100s or 1000s.
Any thoughts?

Réponse acceptée

Matt Fig
Matt Fig le 3 Juin 2011
N = 6;
A = cumsum(tril(ones(N,'single')));
A = A(A>0).';
If memory is a concern, this will conserve it:
L = (N^2+N)/2;
B = ones(1,L);
idx = (1:N).*((1:N)-1)/2;
Li = length(idx);
B(idx(2:Li)) = -(1:Li-1);
B = cumsum(B(L:-1:1));
  2 commentaires
Adam Quintero
Adam Quintero le 3 Juin 2011
I see where the initial sequence length is, and I am wondering how it would work if I wanted the sequence to decrease from the origin.
So:
v= [1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6]
I tried changing the sign of the 6 and the A>0 to A<0, and they didn't work. I think it shows here that I have a lot to learn. I appreciate the patience of anyone with feedback on this.
Thanx
Matt Fig
Matt Fig le 3 Juin 2011
In that case, just reverse the order of two operations:
A = tril(cumsum(ones(N,'single')));

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices 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!

Translated by