Created
February 22, 2016 18:24
-
-
Save visualjeff/b2c8953525a27ffa1d72 to your computer and use it in GitHub Desktop.
Bash Matrix Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
declare -A matrix | |
num_rows=4 | |
num_columns=5 | |
for ((i=1;i<=num_rows;i++)) do | |
for ((j=1;j<=num_columns;j++)) do | |
matrix[$i,$j]=$RANDOM | |
done | |
done | |
f1="%$((${#num_rows}+1))s" | |
f2=" %9s" | |
printf "$f1" '' | |
for ((i=1;i<=num_rows;i++)) do | |
printf "$f2" $i | |
done | |
echo | |
for ((j=1;j<=num_columns;j++)) do | |
printf "$f1" $j | |
for ((i=1;i<=num_rows;i++)) do | |
printf "$f2" ${matrix[$i,$j]} | |
done | |
echo | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment