矩阵“Zigzag”重新排序
我在 MATLAB 中有一个 NxM 矩阵,我想以与 JPEG 重新排序其子块像素的方式类似的方式重新排序:
我希望算法是通用的,这样我就可以传入任何维度的二维矩阵。我是一名 C++ 程序员,很想编写一个老式循环来完成此任务,但我怀疑在 MATLAB 中有更好的方法来完成此任务。
我宁愿想要一个适用于 NxN 矩阵的算法并从那里开始。
例子:
1 2 3
4 5 6 --> 1 2 4 7 5 3 6 8 9
7 8 9
I have an NxM matrix in MATLAB that I would like to reorder in similar fashion to the way JPEG reorders its subblock pixels:
I would like the algorithm to be generic such that I can pass in a 2D matrix with any dimensions. I am a C++ programmer by trade and am very tempted to write an old school loop to accomplish this, but I suspect there is a better way to do it in MATLAB.
I'd be rather want an algorithm that worked on an NxN
matrix and go from there.
Example:
1 2 3
4 5 6 --> 1 2 4 7 5 3 6 8 9
7 8 9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
考虑以下代码:
一个 4x4 矩阵的示例:
以及一个非方矩阵的示例:
Consider the code:
An example with a 4x4 matrix:
and an example with a non-square matrix:
这种方法非常快:
基准测试
以下代码使用 Amro 的出色答案比较运行时间http://es.mathworks.com/help/matlab/ref/timeit.html" rel="nofollow noreferrer">
timeit
。它测试矩阵大小(条目数)和矩阵形状(行数与列数之比)的不同组合。下图是在Windows 7 64位上使用Matlab R2014b获得的。 R2010b 中的结果非常相似。可以看出,新方法将运行时间减少了 2.5(对于小矩阵)和 1.4(对于大矩阵)之间的系数。考虑到条目总数,结果几乎对矩阵形状不敏感。
This approach is pretty fast:
Benchmarking
The following code compares running time with that of Amro's excellent answer, using
timeit
. It tests different combinations of matrix size (number of entries) and matrix shape (number of rows to number of columns ratio).The figure below has been obtained with Matlab R2014b on Windows 7 64 bits. Results in R2010b are very similar. It is seen that the new approach reduces running time by a factor between 2.5 (for small matrices) and 1.4 (for large matrices). Results are seen to be almost insensitive to matrix shape, given a total number of entries.
这是一个非循环解决方案
zig_zag.m
。它看起来很丑,但它有效!:还有一个测试矩阵:
Here's a non-loop solution
zig_zag.m
. It looks ugly but it works!:And a test matrix:
这是一种如何做到这一点的方法。基本上,您的数组是一个 hankel 矩阵加上 1:m 的向量,其中 m 是每个对角线上的元素数量。也许其他人对如何创建必须添加到翻转的汉克尔数组而无需循环的对角数组有一个好主意。
我认为这应该可以推广到非方形数组。
之后,您只需调用
reshape(image(indexMatrix),[],1)
即可获取重新排序元素的向量。编辑
好的,从你的评论来看,你需要像马克建议的那样使用
sort
。请注意,在索引之前您需要先转置输入矩阵,因为 Matlab 首先向下计数,然后向右计数。
Here's a way how to do this. Basically, your array is a hankel matrix plus vectors of 1:m, where m is the number of elements in each diagonal. Maybe someone else has a neat idea on how to create the diagonal arrays that have to be added to the flipped hankel array without a loop.
I think this should be generalizeable to a non-square array.
Afterward, you just call
reshape(image(indexMatrix),[],1)
to get the vector of reordered elements.EDIT
Ok, from your comment it looks like you need to use
sort
like Marc suggested.Note that you'd need to transpose your input matrix first before you index, because Matlab counts first down, then right.
假设 X 是输入 2D 矩阵,并且是方形或横向,这似乎非常有效 -
针对
进行快速运行时测试a href="https://stackoverflow.com/a/28375839/3293881">路易斯的方法 -
Assuming
X
to be the input 2D matrix and that issquare
orlandscape-shaped
, this seems to be pretty efficient -Quick runtime tests against Luis's approach -
让我们假设您有一个与指定正确索引的图像大小相同的二维矩阵。将此数组称为 idx;那么用于重新排序图像的 matlab 命令将是
我没有看到在不使用 for 循环或递归的情况下生成 idx 的明显解决方案,但我会考虑更多。
Let's assume for a moment that you have a 2-D matrix that's the same size as your image specifying the correct index. Call this array idx; then the matlab commands to reorder your image would be
I don't see an obvious solution to generate idx without using for loops or recursion, but I'll think some more.