You are not logged in.

#1 2013-05-28 08:48:21

escuire
Member
Registered: 2013-05-28
Posts: 5

Bash Excercise - simple matrix

How to write script which output will be a matrix like mentioned below?
This is an example for argument = 5, where argument means number of columns and rows. Numbers range could be from 1 - 9.
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7

Thx in advance!

Offline

#2 2013-05-28 08:58:26

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Bash Excercise - simple matrix

What have you tried?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2013-05-28 11:29:53

escuire
Member
Registered: 2013-05-28
Posts: 5

Re: Bash Excercise - simple matrix

So far I managed to display this:
1    2    3    4    5   
6    7    8    9   


#!/bin/bash

# Defining matrix parameters
# array a = ( 1 2 3 4 ... 9 )
a=({1..9})
cols=$1
rows=$1

# Dispalying matrix

for ((row=1; row<=rows; row++)); do
    for ((col=1; col<=cols; col++)); do
       echo -n ${a[((cols*((row-1))+col-1))]}$'\t'
    done
    echo
done

Offline

#4 2013-05-28 11:39:40

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash Excercise - simple matrix

1. Does it have to be just bash or can you use some coreutils?
2. Random numbers between 1 and 9 or just like you showed in your first post: all numbers from 1 to 9 and from 1 again?

Offline

#5 2013-05-28 11:44:44

portix
Member
Registered: 2009-01-13
Posts: 757

Re: Bash Excercise - simple matrix

Try the following:

for ((i=0;i<rows;i++)); do 
    for((j=0;j<cols;j++)); do 
        printf "%d\t" ${a[$(((j+rows*i)%${#a[@]}))]}; 
    done; 
    printf "\n"; 
done

Offline

#6 2013-05-28 11:47:18

escuire
Member
Registered: 2013-05-28
Posts: 5

Re: Bash Excercise - simple matrix

Just bash unfortunately...and it has to be just like I showed. So after 9 should me again 1 and in the next line 2 3 4 5 6, etc. It was example for argument = 5, but it has to work for every number in input.

Offline

#7 2013-05-28 11:48:25

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: Bash Excercise - simple matrix

Is this homework?

It can be done in four clean lines. with only one variable needed.  (Or on one line that would fit on an 80-column terminal).

Last edited by Trilby (2013-05-28 11:52:21)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2013-05-28 11:52:57

escuire
Member
Registered: 2013-05-28
Posts: 5

Re: Bash Excercise - simple matrix

yes, but I am learning by myself. I found this exercise on some webpage. If you see different solution I am open to see that.

Offline

#9 2013-05-28 11:56:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: Bash Excercise - simple matrix

for i in $(seq $(($1*$1))); do
	printf "%d\t" $((($i-1)%9+1))
	[[ $(($i%$1)) -eq 0 ]] && echo
done

EDIT: oops, not quite right, standby. fixed.

Last edited by Trilby (2013-05-28 12:04:01)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#10 2013-05-28 12:04:52

escuire
Member
Registered: 2013-05-28
Posts: 5

Re: Bash Excercise - simple matrix

Thank you for your help. This is what I was looking for:

printf "%d\t" ${a[$(((j+rows*i)%${#a[@]}))]};

Offline

#11 2013-05-28 18:04:54

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Bash Excercise - simple matrix

Please mark your thread as [Solved] by editing your first post and prepending it to the title.

Also, please use code tags when pasting to the boards: https://wiki.archlinux.org/index.php/Fo … s_and_Code


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#12 2013-05-29 07:09:45

args
Member
Registered: 2012-03-06
Posts: 9

Re: Bash Excercise - simple matrix

for i in {1..25};do echo $(($RANDOM%9+1)); done|xargs -n 5

how about this?


Apple hater. Panda lover.

Offline

#13 2013-05-29 08:38:39

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash Excercise - simple matrix

args wrote:
for i in {1..25};do echo $(($RANDOM%9+1)); done|xargs -n 5

how about this?

I asked about this: https://bbs.archlinux.org/viewtopic.php … 9#p1279109
The answer https://bbs.archlinux.org/viewtopic.php … 2#p1279112

Edit: Your code would have to work for any number, not just 5.

Last edited by karol (2013-05-29 08:39:41)

Offline

#14 2013-05-29 09:00:48

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash Excercise - simple matrix

yes "$(seq 9)" | head -n $((5 * 5)) | pr -aT -5

Offline

Board footer

Powered by FluxBB