MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   矩阵“之字形”重新排序 (https://www.labfans.com/bbs/showthread.php?t=23524)

poster 2019-12-10 20:41

矩阵“之字形”重新排序
 
我在MATLAB中有一个NxM矩阵,我希望以与JPEG重新排序其子块像素的方式类似的方式重新排序:

[IMG]https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/JPEG_ZigZag.svg/220px-JPEG_ZigZag.svg.png[/IMG] [URL="http://en.wikipedia.org/wiki/JPEG"](图片来自维基百科)[/URL]

我希望该算法具有通用性,以便可以传递任意尺寸的2D矩阵。我是一名C ++程序员,非常想写一个老式的循环来完成此任务,但是我怀疑在MATLAB中有更好的方法可以做到这一点。

我宁愿想要一个在NxN矩阵上工作并从那里开始的算法。

[B]例: [/B]

1 2 3 4 5 6 --> 1 2 4 7 5 3 6 8 9 7 8 9

[B]回答:[/B]

考虑一下代码:

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

[url=https://stackoverflow.com/questions/3024939]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:02

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.