Home > Back-end >  how to make a random matrix in bash in that program
how to make a random matrix in bash in that program

Time:12-05

#!/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

CodePudding user response:

If you are only looking to display numbers arranged as if they were in a n X n matrix, then the following will give you some ideas on how to get the random numbers and passing those to the "formatting" portion of the script.

#!/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}"
  • Related