在 Erlang 中转置二维矩阵
给定如下所示的矩阵,将其变换 90 度,转换为下面的第二个矩阵。您将如何以最干净的方式做到这一点?首选简短/简洁/清晰的解决方案,其中要点易于掌握。
从
[[A1,A2,A3],
[B1,B2,B3],
[C1,C2,C3]]
到
[[A1,B1,C1],
[A2,B2,C2],
[A3,B3,C3]]
编辑:我意识到原来的问题并不清楚。我想知道如何在 Erlang 中执行此操作。
Given a matrix like below, transform it, say, 90 degrees into the second matrix below. How would you go about doing this in the cleanest way possible? Short/succinct/clear solutions where the point is easy to grasp is preferred.
From
[[A1,A2,A3],
[B1,B2,B3],
[C1,C2,C3]]
To
[[A1,B1,C1],
[A2,B2,C2],
[A3,B3,C3]]
Edit: I realize it was not clear from original question. I'd like to know how to do this in Erlang.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简化已经给出的解决方案,您可以在最短的时间内完成:
Simplifying the solutions already given, you can do it in as short as:
这是我的示例解决方案:
测试:
Here's my sample solution:
Test:
这是我认为我从 Haskell 标准库获得的一个实现:
紧凑且有点令人费解。
Here's an implementation that I think I got from the Haskell standard library:
Compact and slightly mind-bending.
在函数式编程语言中,矩阵转置的常用方法是使用
unzip
。In functional programming languages, the usual approach for matrix transposition is to use
unzip
.您显示的不是矩阵旋转,而是矩阵转置。如果你调用第一个矩阵 A 和第二个 B ,那么你必须
从 A 到 B 你只需要两个嵌套循环,其中 i = 1 到 n 和 j = i+1 到 n ,并且在每次迭代时交换非对角线使用临时变量的条目。
What you are showing is not a matrix rotation, but rather matrix transposition. If you call the first matrix A and the second B then you have
To go from A to B you just need two nested loops with i = 1 to n and j = i+1 to n and at each iteration you swap the off-diagonal entries using a temporary variable.