shell bash程序中随机矩阵生成

xfb7svmp  于 2022-12-04  发布在  Shell
关注(0)|答案(1)|浏览(129)
#!/bin/bash

# How to make a random matrix in bash in that program, 
# I don't understand how to make a random matrix in shell script.

# read the matrix order
read -p "Give rows and columns: " n

# accept elements
echo "Matrix element:"

let i=0
while [ $i -lt $n ]
do
let j=0
while [ $j -lt $n ]
do
read x[$(($n*$i+$j))]
j=$(($j+1))
done
i=$(($i+1))
done
yyhrrdl8

yyhrrdl81#

如果您只想**显示 * a排列在n X n矩阵中的数字,那么下面将为您提供一些关于如何获取随机数并将其传递到脚本的“格式化”部分的想法。

#!/bin/sh

# read the matrix order
read -p "Enter dimension of symmetric matrix: " n

count=$(( $n * $n ))

NUMBERS="./numbers.txt"
shuf -n ${count} --input-range=0-99  >"${NUMBERS}"

# accept elements
echo "Matrix element:"

i=0
while [ $i -lt $n ]
do
    j=0
    while [ $j -lt $n ]
    do
        read pos
        echo "\t${pos}\c"

        j=$(($j+1))
    done
    echo ""
    i=$(($i+1))
done <"${NUMBERS}"

相关问题