how to shift arrays to the right??

for example i have :
arr=[1 0 1 1 0 0 0 1];i want to shift it one step to the right and add a random bit to the left
my array will be ike this arr=[? 1 0 1 1 0 0 0]
?: could be 0 or 1 .

4 commentaires

Easiest way:
bit = (rand > 0.5); %create random bit
arr = [bit, arr(1:end-1)];
Jan
Jan le 21 Jan 2013
Modifié(e) : Jan le 21 Jan 2013
@Matt: Please post this as an answer, such that I can vote for it and mary can accpet this "easiest" soluion.
@mary: The tags are used to classify the questions. As long as almost all questions concern "matlab function"s, this is not a useful tag.
mary
mary le 21 Jan 2013
thanx Mr.Matt it worked
okay Mr.Jan i will consider this note nxt time.
Laeticia Osemeke
Laeticia Osemeke le 20 Oct 2020
How can you do this with a for loop

Connectez-vous pour commenter.

 Réponse acceptée

Thorsten
Thorsten le 21 Jan 2013

0 votes

arr = [round(rand(1,1)) arr];

1 commentaire

okay it worked but didn't delete the shifted bit on the right.. thanx

Connectez-vous pour commenter.

Plus de réponses (1)

Nathan Hall
Nathan Hall le 19 Avr 2022

0 votes

arr = [randi([0,1],1),arr(1:end-1)]

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by