Azzera filtri
Azzera filtri

Shifting Elements of a Vector to the right while deleting the last number

3 visualizzazioni (ultimi 30 giorni)
I have a vector like this:
A = [1 2 3 4 5 0 0 0]
now, I want to shift the elements to the right (lets say beginning from 3), while eliminating the last digit and replacing the 3 with a zero.
desired outcome:
A = [ 1 2 0 3 4 5 0 0]
Is there an easy command to achieve this?
Thank you very much in advance!

Risposta accettata

James Tursa
James Tursa il 10 Lug 2015
Modificato: James Tursa il 10 Lug 2015
A = [1 2 3 4 5 0 0 0] % Original vector
p = 3 % index for first element to shift
z = 1 % number of spots to shift
n = numel(A)
A = [A(1:p-1) zeros(1,min(n-p+1,z)) A(p:n-z)]
A =
1 2 3 4 5 0 0 0
p =
3
z =
1
n =
8
A =
1 2 0 3 4 5 0 0

Più risposte (0)

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by