在Erlang中转置二维矩阵 [英] Transposing a 2 dimensional matrix in Erlang

查看:139
本文介绍了在Erlang中转置二维矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出如下的矩阵,将其变换为下面的第二个矩阵,即90度。你会如何以最干净的方式做到这一点?简单/简洁/清晰的解决方案,其中易于掌握的是首选。



  [[A1,A2,A3],
[B1,B2,B3],
[C1,C2,C3]]

To

  [[A1,B1,C1 ],
[A2,B2,C2],
[A3,B3,C3]]

编辑:我意识到从原始问题不清楚。我想知道如何在 Erlang 中执行此操作。

解决方案

简化已经给出的解决方案你可以简单地做到这一点:

  -module(transp)。 

-export([transpose / 1])。

转置([[] | _]) - > [];
转置(M) - >
[列表:map(fun hd / 1,M)|转置(列表:map(fun tl / 1,M))]。


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.

解决方案

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))].

这篇关于在Erlang中转置二维矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆