Creating A Matrix Using Nested For Loops and Mod function

13 visualizaciones (últimos 30 días)
Maggie Jones
Maggie Jones el 13 de Abr. de 2017
Respondida: Geoff Hayes el 13 de Abr. de 2017
I need to create a n x n matrix function with the following pattern. If n was 4, the matrix should appear like [1 0 2 0;0 3 0 4;5 0 6 0;0 7 0 8]. The hints are given to use for loops and if commands and also that the mod command may be of use. The input should be a single integer value n.

Respuestas (1)

Geoff Hayes
Geoff Hayes el 13 de Abr. de 2017
Maggie - this seems like homework so we can only give out hints but it seems pretty clear what the pattern is: if the row is odd, then you populate the odd numbered columns with a non-zero value. If the row is even, then you populate the even numbered rows with a non-zero value. (Where the non-zero value is just one larger than the last one used.)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by