我尝试使用arrayfun和circshift执行以下操作
s_dfp = magic(4);
s_hh1p = circshift(s_dfp(:,1),[1 -1]);
s_hh2p = circshift(s_dfp(:,2),[1 -2]);
s_hh3p = circshift(s_dfp(:,3),[1 -3]);
s_hh4p = circshift(s_dfp(:,4),[1 -4]);
HH = [s_hh1p s_hh2p s_hh3p s_hh4p];
s_dfp =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
HH =
4 14 15 1
16 2 3 13
5 11 10 8
9 7 6 12
每列都按其列号移动。我想以任意大小来做这件事。
提前致谢。
答案 0 :(得分:1)
将每列向下移动一些等于列号的条目:
[R, C] = size(s_dfp);
row = mod(bsxfun(@plus, (0:R-1).', -(1:C)), R) + 1; %'// shifted row indices
ind = bsxfun(@plus, row, 0:R:numel(s_dfp)-1); %// corresponding linear indices
HH = s_dfp(ind);
示例:
s_dfp =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
给出
HH =
4 7 10 13
16 14 6 8
5 2 15 12
9 11 3 1