What's the best way to create a vector with a repeating and diminutive sequence?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Akzeptierte Antwort
Matt Fig
am 3 Jun. 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 Kommentare
Matt Fig
am 3 Jun. 2011
In that case, just reverse the order of two operations:
A = tril(cumsum(ones(N,'single')));
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!