Matrix transposition: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: improved routine and var names, marked js compatible) |
|||
Line 3,222: | Line 3,222: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|Picat}}== |
|||
Picat has a built-in function transpose/1 (in the util module). |
|||
<lang Picat>import util. |
|||
go => |
|||
M = [[0.0, 0.1, 0.2, 0.3], |
|||
[0.4, 0.5, 0.6, 0.7], |
|||
[0.8, 0.9, 1.0, 1.1]], |
|||
print_matrix(M), |
|||
M2 = [[a,b,c,d,e], |
|||
[f,g,h,i,j], |
|||
[k,l,m,n,o], |
|||
[p,q,r,s,t], |
|||
[u,v,w,z,y]], |
|||
print_matrix(M2), |
|||
M3 = make_matrix(1..24,8), |
|||
print_matrix(M3), |
|||
nl. |
|||
% |
|||
% Print original matrix and its transpose |
|||
% |
|||
print_matrix(M) => |
|||
println("Matrix:"), |
|||
foreach(Row in M) println(Row) end, |
|||
println("\nTransposed:"), |
|||
foreach(Row in M.transpose()) println(Row) end, |
|||
nl. |
|||
% |
|||
% Make a matrix of list L with Rows rows |
|||
% (and L.length div Rows columns) |
|||
% |
|||
make_matrix(L,Rows) = M => |
|||
M = [], |
|||
Cols = L.length div Rows, |
|||
foreach(I in 1..Rows) |
|||
NewRow = new_list(Cols), |
|||
foreach(J in 1..Cols) |
|||
NewRow[J] := L[ (I-1)*Cols + J] |
|||
end, |
|||
M := M ++ [NewRow] |
|||
end.</lang> |
|||
Output: |
|||
<pre>Matrix: |
|||
[0.0,0.1,0.2,0.3] |
|||
[0.4,0.5,0.6,0.7] |
|||
[0.8,0.9,1.0,1.1] |
|||
Transposed: |
|||
[0.0,0.4,0.8] |
|||
[0.1,0.5,0.9] |
|||
[0.2,0.6,1.0] |
|||
[0.3,0.7,1.1] |
|||
Matrix: |
|||
abcde |
|||
fghij |
|||
klmno |
|||
pqrst |
|||
uvwzy |
|||
Transposed: |
|||
afkpu |
|||
bglqv |
|||
chmrw |
|||
dinsz |
|||
ejoty |
|||
Matrix: |
|||
[1,2,3] |
|||
[4,5,6] |
|||
[7,8,9] |
|||
[10,11,12] |
|||
[13,14,15] |
|||
[16,17,18] |
|||
[19,20,21] |
|||
[22,23,24] |
|||
Transposed: |
|||
[1,4,7,10,13,16,19,22] |
|||
[2,5,8,11,14,17,20,23] |
|||
[3,6,9,12,15,18,21,24]</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |