How to shift diffrent rows with diffrent shift positions in matlab?

2 次查看(过去 30 天)
I want to right shift each row of P with different shift position.
P =
1111100100000000
0000100000000000
0000001110000000
1111110010000000
Here, class(P) = char . I want:
P =
1111111111111001
0000000000001000
0000000000000111
0000000111111001
Follwong code i have used for determining the number of shift positions:
[rows cols] = size(P); %4 , 16
x = zeros(rows,cols);
k =1;
for i = 1:rows
c=0;
for m = cols:-1:9 %This loop finds the number of 0s with in 16 to 9 %column for each rows and stops when it gets 1 with in these columns.
if (P(i,m) == '0')
c = c+1;
else (P(i,m) == '1'), break,
end
end
c
end
Here c is the number of right shift position for each rows but how this right shift operation can be performed?
And while shifting, column 1 value of each row (P(:,1)) will be filled instead of zero filling at left side.
How this shift operation can ne performed?

回答(2 个)

Sean de Wolski
Sean de Wolski 2011-12-12
Without looking too much into your code, it looks like circshift might be your friend.
doc circshift

Andrei Bobrov
Andrei Bobrov 2011-12-12
A = ['1111100100000000'
'0000100000000000'
'0000001110000000'
'1111110010000000']
Aout1 = [circshift(A(1:2,:),[ 0 8]);circshift(A(end-[1 0],:),[ 0 7])]
Aout1(1,1:strfind(Aout1(1,:),'011'))='1'
or
A = [A(1:2,[9:end,1:8]);A(3:4,[10:end,1:9])];
A(1,1:8) = '1'

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by