create a matrix using nested for loop

i want to create one matrix of nrow=2, ncol=4. then, using a nested for loop (one inside the other) i want to fill the matrix with numbers, and resulting matrix should be as follows:
[ ,1] [ ,2] [ ,3] [,4]
[1 ,] 2 3 4 5
[2, ] 3 4 5 6

Why do you want to do this? It's not necessary to have a for loop.

1 Like

I know it is not necessary , but i was asked by my lecturer to create the matrix using nested for loop.

Hi @Hossam,
Since your question involves "homework" you should at least make a start at writing some code to achieve your aim. Even if it doesn't work you will learn something, and then people in this forum are more likely to give assistance to correct errors. Try reading some online guides; google "nested for loops in r". Hint: you can fill a matrix either one row at-a-time or one column at-a-time.
HTH

Honestly i tried searching lot , but found no clues :frowning:

I echo what @DavoWW said and advise you take a look at the homework policy.

ok , i started with this code:

W=matrix(2:6,byrow=T,ncol=4)
print(W)

but don't know how to proceed

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.