Is there a function to make a long list of a single value?

6 visualizzazioni (ultimi 30 giorni)
I.e. if I want to make a list 400 values long of all 1's, I would use
ones(400,1)
But what if I wanted to make a list of say, 5's?
Is there a function I can use to do this without using a for loop?

Risposta accettata

Star Strider
Star Strider il 24 Mag 2022
Try this —
Fives = 5*ones(400,1)
Fives = 400×1
5 5 5 5 5 5 5 5 5 5
.
  2 Commenti
David Haydock
David Haydock il 24 Mag 2022
You're a genius! Why didn't I think of that!
Star Strider
Star Strider il 24 Mag 2022
Thank you!
I’m certain you would have in a few minutes. (I’ve been doing this for a while.)
Another option —
Fives = 5+zeros(400,1)
Fives = 400×1
5 5 5 5 5 5 5 5 5 5
.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by