在 Erlang 中转置二维矩阵

发布于 2024-10-25 01:54:57 字数 313 浏览 5 评论 0原文

给定如下所示的矩阵,将其变换 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

分开我的手 2024-11-01 01:54:57

简化已经给出的解决方案,您可以在最短的时间内完成:

-module(transp).

-export([transpose/1]).

transpose([[]|_]) -> [];
transpose(M) ->
  [lists:map(fun hd/1, M) | transpose(lists:map(fun tl/1, M))].

Simplifying the solutions already given, you can do it in as short as:

-module(transp).

-export([transpose/1]).

transpose([[]|_]) -> [];
transpose(M) ->
  [lists:map(fun hd/1, M) | transpose(lists:map(fun tl/1, M))].
灯角 2024-11-01 01:54:57

这是我的示例解决方案:

-module(transp).

-export([transpose/1]).

transpose(L) ->
     transpose_do([], L).

transpose_do(Acc, [[]|_]) ->
     lists:reverse(Acc);
transpose_do(Acc, M) ->
     Row = lists:foldr(
          fun(Elem, FoldAcc) ->
                    [hd(Elem) | FoldAcc]
          end,
          [],
          M),
     transpose_do([Row|Acc], lists:map(fun(X) -> tl(X) end, M)).

测试:

1> M = [[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]].
[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]
2> transp:transpose(M).   
[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3]]

Here's my sample solution:

-module(transp).

-export([transpose/1]).

transpose(L) ->
     transpose_do([], L).

transpose_do(Acc, [[]|_]) ->
     lists:reverse(Acc);
transpose_do(Acc, M) ->
     Row = lists:foldr(
          fun(Elem, FoldAcc) ->
                    [hd(Elem) | FoldAcc]
          end,
          [],
          M),
     transpose_do([Row|Acc], lists:map(fun(X) -> tl(X) end, M)).

Test:

1> M = [[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]].
[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]
2> transp:transpose(M).   
[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3]]
酒几许 2024-11-01 01:54:57

这是我认为我从 Haskell 标准库获得的一个实现:

%% Transpose rows and columns in a list of lists. Works even if sublists
%% are not of same length. Empty sublists are stripped.
transpose([[X | Xs] | Xss]) ->
    [[X | [H || [H | _] <- Xss]]
     | transpose([Xs | [T || [_ | T] <- Xss]])];
transpose([[] | Xss]) -> transpose(Xss);
transpose([]) -> [].

紧凑且有点令人费解。

Here's an implementation that I think I got from the Haskell standard library:

%% Transpose rows and columns in a list of lists. Works even if sublists
%% are not of same length. Empty sublists are stripped.
transpose([[X | Xs] | Xss]) ->
    [[X | [H || [H | _] <- Xss]]
     | transpose([Xs | [T || [_ | T] <- Xss]])];
transpose([[] | Xss]) -> transpose(Xss);
transpose([]) -> [].

Compact and slightly mind-bending.

音栖息无 2024-11-01 01:54:57

在函数式编程语言中,矩阵转置的常用方法是使用unzip

In functional programming languages, the usual approach for matrix transposition is to use unzip.

你的心境我的脸 2024-11-01 01:54:57

您显示的不是矩阵旋转,而是矩阵转置。如果你调用第一个矩阵 A 和第二个 B ,那么你必须

A[i,j] = B[j,i]

从 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

A[i,j] = B[j,i]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文