我在MATLAB中有一个NxM矩阵,我希望以与JPEG重新排序其子块像素的方式类似的方式重新排序:
(图片来自维基百科)
我希望该算法具有通用性,以便可以传递任意尺寸的2D矩阵。我是一名C ++程序员,非常想写一个老式的循环来完成此任务,但是我怀疑在MATLAB中有更好的方法可以做到这一点。
我宁愿想要一个在NxN矩阵上工作并从那里开始的算法。
例:
1 2 3 4 5 6 --> 1 2 4 7 5 3 6 8 9 7 8 9
回答:
考虑一下代码:
M = randi(100, [3 4]); %# input matrix ind = reshape(1:numel(M), size(M)); %# indices of elements ind = fliplr( spdiags( fliplr(ind) ) ); %# get the anti-diagonals ind(:,1:2:end) = flipud( ind(:,1:2:end) ); %# reverse order of odd columns ind(ind==0) = []; %# keep non-zero indices M(ind) %# get elements in zigzag order 一个4x4矩阵的示例:
禄 M M = 17 35 26 96 12 59 51 55 50 23 70 14 96 76 90 15禄 M(ind) ans = 17 35 12 50 59 26 96 51 23 96 76 70 55 14 90 15 以及带有非平方矩阵的示例:
M = 69 9 16 100 75 23 83 8 46 92 54 45 ans = 69 9 75 46 23 16 100 83 92 54 8 45
更多&回答...