I am a bit lost on exercise 4

In one of the comments one is told to: “add row 0 of the new matrix A_ref to the row 2, replacing row 2.” I am not sure how this translates into code. I am trying this: addrows(A, 0,2,1). I am not sure if this is correct. Any hints?

Here is my code: I am not getting the expected output.

def augmented_to_ref(A, b):  
    # stack horizontally matrix A and vector b, which needs to be reshaped as a vector (4, 1)
    A_system = np.hstack((A, b.reshape((4, 1))))
   
    # swap row 0 and row 1 of matrix A_system (remember that indexing in NumPy array starts from 0)
    A_ref = SwapRows(A_system, 0, 1)

    # multiply row 0 of the new matrix A_ref by -2 and add it to the row 1
    A_ref = AddRows(A_ref, 0, 1, -2)

    # add row 0 of the new matrix A_ref to the row 2, replacing row 2
    A_ref = AddRows(A_ref, 0, 2, 1)


    # # multiply row 0 of the new matrix A_ref by -1 and add it to the row 3
    A_ref = AddRows(A_ref, 0, 3, -1)

    # # add row 2 of the new matrix A_ref to the row 3, replacing row 3
    A_ref = AddRows(A_ref, 2, 3, 1)

    # # swap row 1 and 3 of the new matrix A_ref
    A_ref = SwapRows(A_ref, 1, 3)

    # # add row 2 of the new matrix A_ref to the row 3, replacing row 3
    A_ref = AddRows(A_ref, 2, 3, 1)
    

    return A_ref

A_ref = augmented_to_ref(A, b)

print(A_ref)






My output is this:

[[ 1. 2. -1. -1. 3.] [ 0. 1. 4. 3. 22.] [ 0. 4. 1. 1. 17.] [ 0. -1. 4. 4. 17.]]

I am not sure where I am going wrong here.

1 Like

Hello @Edward_Citalan Welcome to the community!

Yes, “add row 0 of the new matrix A_ref to the row 2, replacing row 2.” was a little bit confusing to me too at the beginning, especially the replacing part, but what it means is simply adding both rows and overwriting the result to the row 2.

I have done this exercise some time ago, and I do not remember the details of the addrows function very clearly now. However, I suspect that in addrows(A, 0,2,1) you are adding row 0 and 2 and overwriting the results to row 1.

Hope this solves your confusion.

1 Like

I see. From what I understand if I multiple by one it should work as adding them together. Am I missing something?

def AddRows(M, row_num_1, row_num_2, row_num_1_multiple):
    M_new = M.copy()     
    # multiply row_num_1 by row_num_1_multiple and add it to the row_num_2, 
    # exchanging row_num_2 of the matrix M_new with the result
    M_new[row_num_2] = row_num_1_multiple * M_new[row_num_1] + M_new[row_num_2]
    return M_new
1 Like

Yes, @Edward_Citalan you are absolutely correct. I did not remember the function fully. My apologies.

Do you have any further problems with your code?

1 Like

No problem. Thank you for your help. Yes, I still have problems–it is not outputting the expected output. Not sure why that is the case.

1 Like

Dear @Edward_Citalan if you had problems in this section of your code, I would have suggested line-by-line print debugging. However, from the code you sent, I don’t think there were any problems. Therefore, could you please double check for any mistakes in previous code cells?

Hope you are able to squash the bug right away :wink:

1 Like

hiii…did you solved this problem…I am also stucked here.

1 Like

I did. Where are you stuck?

1 Like

same as you…I am getting same output but not the expected one

1 Like

Did you figure out the solution to this? The problem I’m having is that when I use AddRows in Exercise 4, it adds to the row_num_1 as well as row_num_2, when I’m trying only to exchange row_num_2 while keeping row_num_1 preserved. Is that the problem you were having?

2 Likes

Actually, never mind…I was printing the wrong variable, it works as expected, thanks

1 Like

can you please help me how you did it because i m facing the same issue ?

1 Like

@Edward_Citalan @akanchha_choudhary @Harshtherocking plz share your problem if u haven’t figured it out still as I have just completed my C1_W2_Assignment with 100 % result. I hope I can help you with that.

1 Like

@Atique_Abdullah Hey there, I am facing the issue with addrows to the matrix row and replace it. I don’t know why it is not giving the expected output. Here is my code:

def augmented_to_ref(A, b):    
    ### START CODE HERE ###
    # moderator edit: code removed

    # Note: most lines of code in this function should modify A_ref, as noted in the comments.

    ### END CODE HERE ###
    
    return A_ref

A_ref = augmented_to_ref(A, b)

print(A_ref)

Can you please help me?
Thanks.

1 Like

[/quote]

use A_ref matrix instead of A_system except for where i says use A_system

//////////////////////////////////////////////

multiply row 0 of the new matrix A_ref by -2 and add it to the row 1

A_ref = AddRows(A_system, 0, 1, -2)------------------------------------------> WRONG
A_ref = AddRows(A_ref, 0, 1, -2)------------------------------------------> RIGHT

from here and here onward use “A_ref” not “A_system”

3 Likes

@Atique_Abdullah Thank you so much. It works. But why it don’t work with A_system. I think it don’t work because A_system was a new matrix to every line of code and because of this A_ref was not upgraded with functions. Is that correct @Atique_Abdullah ?

Yes you are right. And I am glad that I was worthy to solve your issue.

2 Likes

All thanks to you. I also scored 100% in my first assignment :smiley:

1 Like

Enjoy the journey ahead. BTW where are u from ?

1 Like

Somewhere from Asia :slight_smile:

1 Like