CS100a Sections

Nov 3-4

Topics

2D array problems

Matlab

For each of these problems, write a segment of Matlab code that solves it. These are the SAME problems that we covered in last week’s section using Java, not Matlab.

1. Write a segment of code that prints the maximum value in row number r

and also prints the number of the column in which it occurs.

2. Write a segment of code that prints the maximum value in column number c

and also prints the number of the row in which it occurs. (no solution included)

3. Write a segment of code that prints the maximum value in all of A and

the postion in which it occurs.

 

4. Write a segment of code that changes A to its transpose. This means

that the rows and columns of A are interchanged, which, for a square matrix

is the same as flipping the values over the main diagonal.

Example: If A is now

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

It should be changed to

1 6 11 16 21

2 7 8 9 10

3 8 13 18 23

4 9 14 19 24

5 10 15 20 25

 

5. Assign B values so that it looks like the first picture of A in question 4 above. Hint: Go through B in row major order counting as you go.

6. Assign B values so that it looks like the second picture of A in question 4 above. Hint: Go through B in column major order counting as you go.

 

7. Search the major diagonal of A to see if it contains any zeros and report your answer. Your search should stop as soon as you discover a zero if there are any. The major diagonal is the one that starts in the upper left corner and runs down to the lower right corner.

8. Do the same as in question 7 but for the minor diagonal (which goes from the upper right corner to the lower left one).

 

 

 

 

Matlab Solutions

»% Here is a Matlab solution to the 8

% questions. You will also find some %calls to the help command so that

% you can see how it works.

% First we will Initialize A to a %random set of integers between 0 and %100.

A = rand(5, 5); % This fills a 5 by 5

%array with random reals

»A

A =

0.1254 0.7362 0.3063 0.4121 0.4679

0.0159 0.7254 0.3510 0.8415 0.2872

0.6885 0.9995 0.5133 0.2693 0.1783

0.8682 0.8886 0.5911 0.4154 0.1537

0.6295 0.2332 0.8460 0.5373 0.5717

»A = ceil(100 .* A);

% Print the contents of A

A

A =

13 74 31 42 47

2 73 36 85 29

69 100 52 27 18

87 89 60 42 16

63 24 85 54 58

»% Assign r and c some arbitrary %values, say 3 and 4

r = 3;

c = 4;

»% Question 1

help max;

MAX Largest component.

For vectors, MAX(X) is the largest element in X. For matrices, MAX(X) is a vector containing the maximum element from each column. [Y,I] = MAX(X) stores the indices of the maximum values in vector I. MAX(X,Y) returns a matrix the same size as X and Y with the largest elements taken from X or Y. When complex, the magnitude MAX(ABS(X)) is used.

See also MIN, MEDIAN, MEAN, SORT.

»%Find the maximum value in row r and %print it

mr = max(A(r, : ))

mr =

100

»% Find all the columns in which it %occurs

help find;

FIND Find indices of the non-zero elements.

I = FIND(X) returns the indices of the vector X that are non-zero. For example, I = FIND(A>100), returns the indices of A where A is greater than 100. See RELOP.

[I,J] = FIND(X) returns the row and column indices of the nonzero entries in the matrix X. This is often used with sparse matrices.

[I,J,V] = FIND(X) also returns a column vector of the nonzero entries in X. Note that find(X) and find(X~=0) will produce the same I and J, but the latter will produce a V with all 1's.

 

 

»find(A(r, : ) == mr)

% here we look in row r for the max %value just calculated

ans =

2

»% Question 2

% Find the maximum value in column c %and print it

mc = max(A( : , c))

mc =

85

»% Find all the rows in which it occurs

find(A( : , c) == mc)

% here we look in column c for the max %value just calculated

ans =

2

»% Question 3

% Find the max over all of A

mall = max(max(A))

% note - max(A) is a row vector %containing the max value in each %column, so we need max(max(A))

mall =

100

»% Find where it occurs

[I, J] = find(A == mall)

I =

3

J =

2

»% Question 4

% This one is easy

A = A';

A

A =

13 2 69 87 63

74 73 100 89 24

31 36 52 60 85

42 85 27 42 54

47 29 18 16 58

 

»%skip Questions 5 and 6 until we know % more about creating 2D arrays in %Matlab

 

 

% Question 7

help diag

DIAG Create or extract diagonals.

If V is a row or column vector with N components, DIAG(V,K) is a square matrix of order N+ABS(K) with the elements of V on the K-th diagonal. K = 0 is the main diagonal, K > 0 is above the main diagonal and K < 0 is below the main diagonal. DIAG(V) simply puts V on the main diagonal. For example, DIAG(-M:M) + DIAG(ONES(2*M,1),1) + DIAG(ONES(2*M,1),-1) produces a tridiagonal matrix of order 2*M+1.

If X is a matrix, DIAG(X,K) is a column vector formed from the elements of the K-th diagonal of X. DIAG(X) is the main diagonal of X. DIAG(DIAG(X)) is a diagonal matrix.

»% First store the diagonal of A in the %vector v

v = diag(A);

v

 

 

 

v =

13

73

52

42

58

»% Now display all the places where it %is zero.If there are none the answer %will be the null matrix.

w = find(v == 0)

w =

[]

 

»% Since this gave us the null matrix %let's change A a bit and try again.

A(3,3) = 0;

A(5,5) = 0;

v = diag(A);

w = find(v == 0)

w =

3

5

 

»% Skip Question 8 until next week as well…