how to shift rows to right and left of matrix ?

15 次查看(过去 30 天)
I want to shift the rows of the matrix to the right if A<B with shift step numbers c or left if A>B with shift step numbers c
v = [1,2 ,3;4,5,6;7,8,9];
[m,n]=size(v);
A=[3 2 1];
B=[2 5 2];
c=[4 1 3];%shift step number c
for i=1:m
for j=1:n
if A(i)<B(i) %shift row to right with shift step number c
p(i,j)=circshift(v(i,j),c(i),2)
else
p(i,j)=circshift(v(i,j),-c(i),2) %shift row to left with shift step number c
end
end
end

采纳的回答

Stephen23
Stephen23 2021-8-21
M = [1,2,3;4,5,6;7,8,9]
M = 3×3
1 2 3 4 5 6 7 8 9
A = [3,2,1];
B = [2,5,2];
c = [4,1,3];
for k = 1:size(M,1)
if A(k)<B(k) %shift row to right with shift step number c
M(k,:) = circshift(M(k,:),c(k));
else %shift row to left with shift step number c
M(k,:) = circshift(M(k,:),-c(k));
end
end
M
M = 3×3
2 3 1 6 4 5 7 8 9

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by