Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Multiply elements of 2 arrays together into another array Post 303034202 by sarapham409 on Sunday 21st of April 2019 07:51:39 PM
Old 04-21-2019
Multiply elements of 2 arrays together into another array

So I need to Write an array processing program using a Linux shell programming language to perform the following.
Load array X of 20 numbers from an input file X.
Load array Y of 20 numbers from an input file Y.
Compute array Z by multiply Xi * Yi then compute the square-root of this computation.

What I have done so far is loading the input file X.txt and Y.txt into array X & Y. I try to compute the multiplication and insert it into array Z but I keep getting a syntax error. This complicates me from moving on to compute the square root of the elements in Z.

Code:
#!/bin/bash

oldIFS="$IFS"
IFS=$'\n' X=($(<X.txt))
IFS="$oldIFS"

for each in "${X[@]}"
do
	echo "$each"
done

count=${#X[@]}

oldIFS="$IFS"
IFS=$'\n' Y=($(<Y.txt))
IFS="$oldIFS"

for each in "${Y[@]}"
do
	echo "$each"
done


for ((i=0;i<count;i++));do
	Z[$i]=$("${X[$i]} * ${Y[$i]}")
done

for each in "${Z[@]}"
do
	echo "$each"
done

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To return the elements of array

Hi, Please can someone help to return the array elements from a function. Currently the problem I face is that tempValue stores the value in myValue as a string while I need an array of values to be returned instead of string. Many Thanks, Sudhakar the function called is: ... (5 Replies)
Discussion started by: Sudhakar333
5 Replies

2. Shell Programming and Scripting

PHP arrays as array elements

PHP question...I posted this on the Web Development forum, but maybe this is a better place! I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person... (3 Replies)
Discussion started by: JerryHone
3 Replies

3. Shell Programming and Scripting

How to access the elements of two arrays with a single loop using the inbuilt index.

Hi all, I wanted to access two arrays (of same size) using one for loop. Ex: #!/bin/bash declare -a num declare -a words num=(1 2 3 4 5 6 7) words=(one two three four five six seven) for num in ${num} do echo ":$num: :${words}:" done Required Output: :1: :one: (11 Replies)
Discussion started by: 14341
11 Replies

4. Shell Programming and Scripting

Array with String Elements

How can I get my array to understand the double-quotes I'm passing into it are to separate text strings and not part of an element? here's what I'm working with... db2 -v connect to foo db2 -x "select '\"' || stats_command || '\",' from db2law1.parallel_runstats where tabname = 'BAZ'" set... (4 Replies)
Discussion started by: djschmitt
4 Replies

5. Shell Programming and Scripting

Removing elements from an array

Hi I have two arrays : @arcb= (450,625,720,645); @arca=(625,645); I need to remove the elements of @arca from elements of @arcb so that the content of @arcb will be (450,720). Can anyone sugget me how to perform this operation? The code I have used is this : my @arcb=... (3 Replies)
Discussion started by: rkrish
3 Replies

6. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

7. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

8. Shell Programming and Scripting

Compare multiple arrays elements using awk

I need your help to discover missing elements for each box. In theory each box should have 4 items: ITEM01, ITEM02, ITEM08, and ITEM10. Some boxes either have a missing item (BOX02 ITEM08) or might have da duplicate item (BOX03 ITEM02) and missing another one (BOX03 ITEM01). file01.txt ... (2 Replies)
Discussion started by: alex2005
2 Replies

9. Shell Programming and Scripting

Sum elements of 2 arrays excluding labels

I'm looking for an efficient way to sum elements from 2 arrays using AWK and preserve header as well as sample names in the output array. I have Ubuntu 16.04 LTS. For example; ARRAY 1 SAMPLE DERIVED ANCESTRAL Sample1 14352 0 Sample2 14352 0 Sample3 14352 0 Sample4 ... (8 Replies)
Discussion started by: Geneanalyst
8 Replies

10. UNIX for Beginners Questions & Answers

Add column and multiply its result to all elements of another column

Input file is as follows: 1 | 6 2 | 7 3 | 8 4 | 9 5 | 10 Output reuired (sum of the first column $1*$2) 1 | 6 | 90 2 | 7 | 105 3 | 8 | 120 4 |9 | 135 5 |10 | 150 Please enclose sample input, sample output, and code... (5 Replies)
Discussion started by: Sagar Singh
5 Replies
DLAR1V(l)								 )								 DLAR1V(l)

NAME
DLAR1V - compute the (scaled) r-th column of the inverse of the sumbmatrix in rows B1 through BN of the tridiagonal matrix L D L^T - sigma I SYNOPSIS
SUBROUTINE DLAR1V( N, B1, BN, SIGMA, D, L, LD, LLD, GERSCH, Z, ZTZ, MINGMA, R, ISUPPZ, WORK ) INTEGER B1, BN, N, R DOUBLE PRECISION MINGMA, SIGMA, ZTZ INTEGER ISUPPZ( * ) DOUBLE PRECISION D( * ), GERSCH( * ), L( * ), LD( * ), LLD( * ), WORK( * ), Z( * ) PURPOSE
DLAR1V computes the (scaled) r-th column of the inverse of the sumbmatrix in rows B1 through BN of the tridiagonal matrix L D L^T - sigma I. The following steps accomplish this computation : (a) Stationary qd transform, L D L^T - sigma I = L(+) D(+) L(+)^T, (b) Progressive qd transform, L D L^T - sigma I = U(-) D(-) U(-)^T, (c) Computation of the diagonal elements of the inverse of L D L^T - sigma I by combining the above transforms, and choosing r as the index where the diagonal of the inverse is (one of the) largest in magnitude. (d) Computation of the (scaled) r-th column of the inverse using the twisted factorization obtained by combining the top part of the the stationary and the bottom part of the progressive transform. ARGUMENTS
N (input) INTEGER The order of the matrix L D L^T. B1 (input) INTEGER First index of the submatrix of L D L^T. BN (input) INTEGER Last index of the submatrix of L D L^T. SIGMA (input) DOUBLE PRECISION The shift. Initially, when R = 0, SIGMA should be a good approximation to an eigenvalue of L D L^T. L (input) DOUBLE PRECISION array, dimension (N-1) The (n-1) subdiagonal elements of the unit bidiagonal matrix L, in elements 1 to N-1. D (input) DOUBLE PRECISION array, dimension (N) The n diagonal elements of the diagonal matrix D. LD (input) DOUBLE PRECISION array, dimension (N-1) The n-1 elements L(i)*D(i). LLD (input) DOUBLE PRECISION array, dimension (N-1) The n-1 elements L(i)*L(i)*D(i). GERSCH (input) DOUBLE PRECISION array, dimension (2*N) The n Gerschgorin intervals. These are used to restrict the initial search for R, when R is input as 0. Z (output) DOUBLE PRECISION array, dimension (N) The (scaled) r-th column of the inverse. Z(R) is returned to be 1. ZTZ (output) DOUBLE PRECISION The square of the norm of Z. MINGMA (output) DOUBLE PRECISION The reciprocal of the largest (in magnitude) diagonal element of the inverse of L D L^T - sigma I. R (input/output) INTEGER Initially, R should be input to be 0 and is then output as the index where the diagonal element of the inverse is largest in mag- nitude. In later iterations, this same value of R should be input. ISUPPZ (output) INTEGER array, dimension (2) The support of the vector in Z, i.e., the vector Z is nonzero only in elements ISUPPZ(1) through ISUPPZ( 2 ). WORK (workspace) DOUBLE PRECISION array, dimension (4*N) FURTHER DETAILS
Based on contributions by Inderjit Dhillon, IBM Almaden, USA Osni Marques, LBNL/NERSC, USA LAPACK version 3.0 15 June 2000 DLAR1V(l)
All times are GMT -4. The time now is 11:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy