How to circ shift rows by different increments

21 ビュー (過去 30 日間)
hbcukid
hbcukid 2021 年 2 月 22 日
コメント済み: hbcukid 2021 年 2 月 24 日
I have a 100x100 matrix and I am trying to figure how to shift them in different increments. For example I want row 1 to shift by none, row 2 by 10, row 3 by 20, row 4 by 30, and row 5 by 40 and for it to repeat this process every 5 rows. How can I achieve this?

採用された回答

hbcukid
hbcukid 2021 年 2 月 23 日
編集済み: hbcukid 2021 年 2 月 24 日
A = rand(100,100);
Fill_Value = 0;
for Target_Group = 1: +5: length(A)
for Relative_Row = 0: 4
Row = Target_Group+Relative_Row;
Shift_Value = Relative_Row*10;
A(Row,:) = [Fill_Value*ones(1,Shift_Value) A(Row,1:end-Shift_Value)];
end
end
  2 件のコメント
Stephen23
Stephen23 2021 年 2 月 24 日
Simpler:
B = zeros(size(A));
for k = 1:size(A,1)
x = mod(k-1,5)*10;
B(k,1+x:end) = M(k,1:end-x);
end
hbcukid
hbcukid 2021 年 2 月 24 日
Oh wow thanks

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

その他の回答 (1 件)

Jan
Jan 2021 年 2 月 22 日
編集済み: Jan 2021 年 2 月 22 日
What does "shift" mean? Circular shifting the elements? Adding a value to the contents of the elements?
A = rand(100, 100);
s = [0, 10, 20, 30, 40];
% For circular shifting the elements:
for k = 1:100
sk = s(mod(k - 1, 5)) + 1; % 1:100 -> 1,2,3,4,5,1,2,3,4,5,...
A(k, :) = circshift(A(k, :), sk);
end
% Or if "shift" means adding to the values is meant:
A = A + repmat(s(:), 20, 1);
Did this solve your homework? If so: Sorry. Please mention explicitly, if a question conserns homework, such that we can give useful hinbts instead of a solution.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by