Matrix transposition: Difference between revisions

added Icon/Unicon example
m (→‎{{header|MATLAB}}: "'" was not obvious, added spaces to improve readability.)
(added Icon/Unicon example)
Line 845:
3.3 7.7
4.4 8.8 </lang>
 
=={{header|Icon}} and {{header|Unicon}}==
 
<lang Icon>
procedure transpose_matrix (matrix)
result := []
# for each column
every (i := 1 to *matrix[1]) do {
col := []
# extract the number in each row for that column
every (row := !matrix) do put (col, row[i])
# and push that column as a row in the result matrix
put (result, col)
}
return result
end
 
procedure print_matrix (matrix)
every (row := !matrix) do {
every writes (!row || " ")
write ()
}
end
 
procedure main ()
matrix := [[1,2,3],[4,5,6]]
write ("Start:")
print_matrix (matrix)
transposed := transpose_matrix (matrix)
write ("Transposed:")
print_matrix (transposed)
end
</lang>
 
Output:
<pre>
Start:
1 2 3
4 5 6
Transposed:
1 4
2 5
3 6
</pre>
 
=={{header|IDL}}==
342

edits