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 Comments

Easiest way:
bit = (rand > 0.5); %create random bit
arr = [bit, arr(1:end-1)];
Jan
Jan on 21 Jan 2013
Edited: Jan on 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.
thanx Mr.Matt it worked
okay Mr.Jan i will consider this note nxt time.
How can you do this with a for loop

Sign in to comment.

 Accepted Answer

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

1 Comment

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

Sign in to comment.

More Answers (1)

Nathan Hall
Nathan Hall on 19 Apr 2022
arr = [randi([0,1],1),arr(1:end-1)]

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 21 Jan 2013

Answered:

on 19 Apr 2022

Community Treasure Hunt

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

Start Hunting!