how to shift rows to right and left of matrix ?

15 ビュー (過去 30 日間)
elor hdd
elor hdd 2021 年 8 月 21 日
コメント済み: elor hdd 2021 年 8 月 21 日
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 Center および File ExchangeStructures についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by