LoginSignup
1
1

More than 3 years have passed since last update.

【Python】転置行列を内包表記で求める

Posted at

転置行列について

A = [
    [a1_1, a1_2, ..., a1_W],
    [a2_1, a2_2, ..., a2_W],
       :     :    :     :
    [aH_1, aH_2, ..., aH_W]
]

上のような行列Aを、

At = [
    [a1_1, a2_1, ..., aH_1],
    [a1_2, a2_2, ..., aH_2],
       :     :    :     :
    [a1_W, a2_W, ..., aH_W]
]

上のAtのように変形させた行列のことを指します。

実装

displace.py
A = [
    [1, 2], 
    [3, 4], 
    [5, 6]
]

H = 3
W = 2

At = [[A[i][j] for i in range(H)] for j in range(W)]

print(*A, sep="\n")
print("")
print(*At, sep="\n")

実行結果

 > displace.py
[1, 2]
[3, 4]
[5, 6]

[1, 3, 5]
[2, 4, 6]

成功。

1
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1