How to shift elements in a row matrix?

Suppose i have a vector x=[1 1 1 1 0 0 1 1] i want 1 bit shifting such that the output is x1=[0 1 1 1 1 0 0 1] x2=[0 0 1 1 1 1 0 0]
How to do this??

 採用された回答

KL
KL 2017 年 10 月 20 日

0 投票

If you have access to Fixed-Point Designer toolbox, you could use bitsll

9 件のコメント

Darsana P M
Darsana P M 2017 年 10 月 20 日
Actually i tried this function, w=[1 1 1 1 1 1 1 1]; k= bitsll(w,1);
For this output for k is: k =
2 2 2 2 2 2 2 2
This same is for circshift(), bitshift() and so on. How to solve this problem??
KL
KL 2017 年 10 月 20 日
編集済み: KL 2017 年 10 月 20 日
Wait! I got you all wrong. Yours simply a vector with 1s and 0s. I misread them as binary. "bit shifting" is not the right word.
Anyway, try the following code,
x=[1 1 1 1 0 0 1 1];
movex = @(z,a) [zeros(1,a) z(1:end-a)];
x1 = movex(x,1)
x2 = movex(x,2)
Darsana P M
Darsana P M 2017 年 10 月 20 日
Thanks a lot. Now it seems more better. Please do help me in 1 point. I did not understand this line : movex = @(z,a) [zeros(1,a) z(1:end-a)];
If i need to do left shift, what must i do?
KL
KL 2017 年 10 月 21 日
[zeros(1,a) z(1:end-a)]
if you read this, it adds 'a' number of zeros in the beginning and then neglects 'a' number of elements from z from behind. Do you understand? To do it the opposite way, you just need to modify it a tiny bit. Why don't you try and show me with how you tried?
Darsana P M
Darsana P M 2017 年 10 月 21 日
[z(1:end-a) zeros(1,a)] Is this the way?
KL
KL 2017 年 10 月 22 日
Almost, but this time, you'd want to neglect the element from the beǵinning, so it should be,
[z(a+1:end) zeros(1,a)]
Darsana P M
Darsana P M 2017 年 10 月 22 日
On more doubt, x1 dimension is 1:8 but dimension of x2 redues to 1:7 For comparison purposes both dimensions must be same right, so what must be done?
KL
KL 2017 年 10 月 22 日
All of it should be of the same size, yes! If you used the code I showed you above, you'd get the result of exactly the same size.
Darsana P M
Darsana P M 2017 年 10 月 22 日
Yes. thank you sir

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2017 年 10 月 20 日

3 投票

x1=zeros(size(x));
x1(2:end)=x(1:end-1)

4 件のコメント

Darsana P M
Darsana P M 2017 年 10 月 20 日
Thanks for the response. But this case cannot be done for all the cases know? How can we generalize this shifting operation??
Matt J
Matt J 2017 年 10 月 20 日
Why not?
Darsana P M
Darsana P M 2017 年 10 月 21 日
Then could you please exlain these steps: x1=zeros(size(x)); x1(2:end)=x(1:end-1); By the first step it means adding zeros according to the size of x. But i didnot understand the second step.
Anu
Anu 2018 年 8 月 25 日
i want this code to execute for every iterations.I need to run it for 64 times and i need to display from 1to 64.how could i do this?

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2017 年 10 月 20 日

コメント済み:

Anu
2018 年 8 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by