Question

Why the answer is D? Explain the all process.

23. Which of the following is the expected output of the command below? > X <- matrix(0, 3, 3) > for (i in 1:3) for (j if in 1:3) t ((j-i)<1) { [1, 01 [2,] 1 0 [3,] -2 1 0 2 [1,1 01 [2,]0 01 [3,] 0 0 0 2 [1,] 0 0 0 [2,] 1 00 [3,] 210 [1,] 0 0 0 [2,]0 0 -1 [3,] 0 00 Answer: d

0 0
Add a comment Improve this question Transcribed image text
Answer #1

First of all x matrix is defined as :

x=10 0 0

Now, in the first iteration i = 1 and j = 1

if condition is checked ( j - i ) < 1

( 1 - 1 ) < 1

0 < 1 which is true

Hence, 1st row and 1st column element is x[1,1] = ( j - i ) = 1 - 1 = 0

Matrix X remains unchanged.

Now, in the second iteration i = 1 and j = 2

if condition is checked ( j - i ) < 1

( 2 - 1 ) < 1

1 < 1 which is false

Hence, 1st row and 2nd column element remains unchanged.

Matrix X remains unchanged.

Now, in the third iteration i = 1 and j = 3

if condition is checked ( j - i ) < 1

( 3 - 1 ) < 1

2 < 1 which is false

Hence, 1st row and 3rd column element remains unchanged.

Matrix X remains unchanged.

Now, in the fourth iteration i = 2 and j = 1

if condition is checked ( j - i ) < 1

( 1 - 2 ) < 1

-1 < 1 which is true

Hence, 2nd row and 1st column element is x[2,1] = ( j - i ) = 1 - 2 = -1

Matrix X becomes :

X = \begin{bmatrix} 0 & 0 & 0\\ -1 & 0 & 0\\ 0 & 0& 0 \end{bmatrix}

Now, in the fifth iteration i = 2 and j = 2

if condition is checked ( j - i ) < 1

( 2 - 2 ) < 1

0 < 1 which is true

Hence, 2nd row and 2nd column element is x[2,2] = ( j - i ) = 2 - 2 = 0

Matrix X remains unchanged.

Now, in the sixth iteration i = 2 and j = 3

if condition is checked ( j - i ) < 1

( 3 - 2 ) < 1

1 < 1 which is false

Hence, 2nd row and 3rd column element remains unchanged.

Matrix X remains unchanged.

Now, in the seventh iteration i = 3 and j = 1

if condition is checked ( j - i ) < 1

( 1 - 3 ) < 1

-2 < 1 which is true

Hence, 3rd row and 1st column element is x[3,1] = ( j - i ) = 1 - 3 = -2

Matrix X becomes :

X = \begin{bmatrix} 0 & 0 & 0\\ -1 & 0 & 0\\ -2 & 0& 0 \end{bmatrix}

Now, in the eighth iteration i = 3 and j = 2

if condition is checked ( j - i ) < 1

( 2 - 3 ) < 1

-1 < 1 which is true

Hence, 3rd row and 2nd column element is x[3,2] = ( j - i ) = 2 - 3 = - 1

Matrix X becomes :

X = \begin{bmatrix} 0 & 0 & 0\\ -1 & 0 & 0\\ -2 & -1& 0 \end{bmatrix}

Now, in the ninth iteration i = 3 and j = 3

if condition is checked ( j - i ) < 1

( 3 - 3 ) < 1

0 < 1 which is true

Hence, 3rd row and 3rd column element is x[3,3] = ( j - i ) = 3 - 3 = 0

Matrix X remains unchanged.

So, finally the X matrix is obtained as :

X = \begin{bmatrix} 0 & 0 & 0\\ -1 & 0 & 0\\ -2 & -1& 0 \end{bmatrix}

Add a comment
Know the answer?
Add Answer to:
Why the answer is D? Explain the all process. 23. Which of the following is the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT