CS1001 Lecture 23 - PowerPoint PPT Presentation

About This Presentation
Title:

CS1001 Lecture 23

Description:

m x n matrix addition and subtraction is done element by corresponding element ... Matrix Add/Subtract (Cont'd) ! Add M and N, Subtract N from M. DO I = 1,2. DO ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 16
Provided by: plum1
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: CS1001 Lecture 23


1
CS1001 Lecture 23
  • Quiz 5
  • Multi-Dimensional Array

2
Quiz 5 -- 1
DO I 1,5 IF (MOD(I,2) 0) THEN
Number(I) 2 I ELSE Number(I) 2
I1 END IF END DO
Third time in loop I 3 MOD(3,2) 1
.NE. 0 so - ELSE Number(1)
2317 Fourth time in loop I 4 MOD(4,2)
0 .EQ. 0 so - THEN Number(2) 24
8 Fifth time in loop I 5 MOD(5,2) 1
.NE. 0 so - ELSE Number(1) 25111
First time in loop I 1 MOD(1,2) 1
.NE. 0 so - ELSE Number(1)
2113 Second time in loop I 2 MOD(2,2)
0 .EQ. 0 so - THEN Number(2) 22 4
3
Quiz 5 -- 2
I 0 DO I I 1 Number (I) MIN (
I,3) IF (I gt 5) EXIT END DO
Third time in loop I 2 1 3 Number (3)
MIN ( 3,3) 3 (3 gt 5) is .FALSE. Do not
EXIT Fourth time in loop I 3 1 4 Number
(4) MIN ( 4,3) 3 (4 gt 5) is .FALSE. Do
not EXIT Fifth time in loop I 4 1 5 Number
(5) MIN ( 5,3) 3 (5 gt 5) is .TRUE.
EXIT
I 0 First time in loop I 0 1 1 Number
(1) MIN ( 1,3) 1 (1 gt 5) is .FALSE. Do
not EXIT Second time in loop I 1 1 Number
(2) MIN ( 2,3) 2 (2 gt 5) is .FALSE. Do
not EXIT
4
Multi-Dimension Arrays
  • Basically the same as one-dimensional
  • Declared as
  • REAL, DIMENSION(4, 3, 7) Temp
  • or
  • REAL, DIMENSION(14, 13, 17) Temp
  • or
  • REAL Temp(14, 13, 17)
  • An element is Temp(1, 3, 2)
  • Multi-Dimensional Array Apps
  • Tables of data
  • Matrix processing

5
2-Dimensional Array
  • Declare and dimension
  • INTEGER,DIMENSION(19,13) iVar1, iVar2
  • REAL, DIMENSION(025,15) rVar1, rVar2
  • Initialize whole array
  • iVar1 0
  • rVar1 9.99
  • Reference elements using subscript
  • iVar2(8,2) 25
  • rVar2(19,5) 17.49

6
Array Processing -- storage
(1,1) (1,2) (1,3) (2,1) (2,2) (2,3)
?
  • Rowwise
  • Table(1,1)
  • Table(1,2)
  • Table(1,3)
  • Table(2,1)
  • Table(2,2)
  • etc.

Columnwise Table(1,1) Table(2,1) Table(3,1) Table
(1,2) Table(2,2) etc. Fortran convention is
columnwise
7
Matrix Processing
  • m x n matrix addition and subtraction is done
    element by corresponding element
  • INTEGER M(2,3),N(2,3),Add(2,3),Sub(2,3)
  • ! Remember this is done columnwise!
  • READ , M
  • READ , N
  • ! Let the computer do the work and handle all
  • ! of the overhead
  • Add M N
  • Sub M - N
  • PRINT , Add
  • PRINT , Sub
  • ! The output isnt pretty, but its right

M(1,1) M(2,1) M(1,2) M(2,2) M(1,3) M(2,3)
Add(1,1) Add(2,1) Add(1,2) Add(2,2) Add(1,3)
Add(2,3)
8
Matrix Addition
  • INTEGER M(2,3),N(2,3),Add(2,3),Sub(2,3), I,J
  • ! READ elements of the 2 arrays!
  • DO I 1,2
  • DO J 1,3
  • READ , M(I,J)
  • END DO
  • END DO
  • DO I 1,2
  • DO J 1,3
  • READ , N(I,J)
  • END DO
  • END DO

Input M(1,1) M(1,2) M(1,3) M(2,1) M(2,2) M(2,3) N
(1,1) N(1,2) N(1,3) N(2,1) N(2,2) N(2,3)
  • ! Implied Loop
  • DO I 1,2
  • READ , ((M(I,J), J1,3)
  • END DO
  • Input
  • M(1,1) M(1,2) M(1,3)
  • M(2,1) M(2,2) M(2,3)

9
Matrix Add/Subtract (Contd)
! Add M and N, Subtract N from M DO I 1,2 DO
J 1,3 Add(I,J) M(I,J)N(I,J) END DO END
DO DO I 1,2 DO J 1,3 Add(I,J)
M(I,J)-N(I,J) END DO END DO ! Print results
using Implied Do-Loop DO I 1,2 PRINT ,
(Add(I,J), J1,3) END DO DO I 1,2 PRINT ,
(Sub(I,J), J1,3) END DO
I 1 J1 Add(1,1) M(1,1)N(1,1) J2
Add(1,2) M(1,2)N(1,2) J3 Add(1,3)
M(1,3)N(1,3) I 2 J1 Add(2,1)
M(2,1)N(2,1) J2 Add(2,2) M(2,2)N(2,2)
J3 Add(2,3) M(2,3)N(2,3)
I 1 Add(1,1) Add(1,2) Add(1,3) I 2
Add(2,1) Add(2,2) Add(2,3)
10
Matrix Multiplication
  • The number of columns of A must be equal to the
    number of rows of B to be able to multiply them
  • 22 14 06 29 2 12 04 20
  • 48 34 06 49 6 32 04 40

4 2 5 3 6 4 1 8 9 0 0 2
1 0 2 3 0 4
22 2 5 7 48 6 15 17


11
Intrinsic Array-Processing Subprograms
  • MATMUL(A,B) - returns the matrix product of
    matrices A and B.
  • MAXVAL(A,D) - returns an array of one fewer
    dimension than A containing the max values in
    array A along dimension D. If D is omitted, the
    max value of the whole array is returned.
  • For example

MAXVAL(A,2)
A
MAXVAL(A,1)
4 2 5 3 6 4 1 8 9 0 0 2
9 4 5 8
5 8 9
MAXVAL(A) 9
12
PROGRAM Finding_Row_Maxima_1 !--------------------
--------------------------------- ! Program to
read an array of numbers and to find ! the
maximum value in ! each row using the subroutine
FindRowMaxima. ! Identifiers used are !
IntArray two-dimensional array of numbers !
NumRows number of rows in IntArray
(constant) ! NumColumns number of columns in
IntArray (constant) ! FindRowMaxima subroutine
to find the row maxima ! I, J
subscripts ! ! Input NumRows, NumColumns, and
IntArray ! Output The maximum value in each row
! (using FindRowMaxima) !-------------
-------------------------------------
13
IMPLICIT NONE INTEGER, PARAMETER NumRows
4, NumColumns 5 INTEGER, DIMENSION(NumRows,
NumColumns) IntArray INTEGER I, J
PRINT , "Enter the", NumRows, "x", NumColumns,
"array of integers in a rowwise
manner" READ , ((IntArray(I,J), J 1,
NumColumns), I 1, NumRows) CALL
FindRowMaxima(IntArray) CONTAINS
Enter the 4 x 5 array of integers in a rowwise
manner 1 2 3 4 5 6 6 6 6 6 4 3 2 1 0 2 9 4 8 6
14
!-FindRowMaxima--------------------------------
-- ! Subroutine to find and display the maximum
value ! in each row of an array Table of
integers. ! Local identifiers used are !
Rows number of rows in array Table !
(named constant) ! Cols number of
columns in array Table ! (named
constant) ! RowMax one-dimensional array of
row maxima ! I subscript ! !
Accepts Array Table ! Output The maximum
value in each row of Table !--------------------
-----------------------------
15
SUBROUTINE FindRowMaxima(Table) INTEGER,
PARAMETER Rows 4, Cols 5 INTEGER,
DIMENSION(Rows, Cols), INTENT(IN) Table
INTEGER, DIMENSION(Rows) RowMax INTEGER
I ! Find max values along dimension 2
RowMax MAXVAL(Table, 2) DO I 1, Rows
PRINT 130, I, RowMax(I) 130 FORMAT
(1X,"Maximum entry in row", I2, "is", I2) END
DO END SUBROUTINE FindRowMaxima END
PROGRAM Finding_Row_Maxima_1
Maximum entry in row 1 is 5 Maximum entry in
row 2 is 6 Maximum entry in row 3 is 4
Maximum entry in row 4 is 9
Write a Comment
User Comments (0)
About PowerShow.com