How to build a row vector with a loop?

Hello everyone, I am a beginner to matlab and I need help trying to build a row vector on matlab. I have a variable NL that is changed manually each time, but I need y0 to match in this fashion.
For example, if NL=2, y0= [1 0]. y0 should be the length of NL, but the first element should be a 1.
Here is another example, if NL=4, y0= [1 0 0 0].
I would like to be able to do this with a for loop for an arbitrary value of NL, here is my code so far.
Can anyone help me with this please?
Thank you.

6 commentaires

NL = 8;
y0 = zeros(1,NL);
y0(1) = 1.0;
y0
y0 = 1×8
1 0 0 0 0 0 0 0
Karen Smith
Karen Smith le 28 Juil 2022
thank you!!!
Another way just to illustrate --
NL = 8;
y0 =[1 zeros(1,NL-1)];
You can dream up any number of alternatives...
NL = 8;
y0 = 1;
y0(NL) = 0;
y0
y0 = 1×8
1 0 0 0 0 0 0 0
Yes. Which can also be reversed in sequence of course --
NL = 8;
y0(NL) = 0;
y0(1) = 1;
Yes. Or vectorized --
NL = 8;
y0([1 NL]) = [1 0];
y0
y0 = 1×8
1 0 0 0 0 0 0 0
clear y0
y0([NL 1]) = [0 1];
y0
y0 = 1×8
1 0 0 0 0 0 0 0

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Commenté :

le 29 Juil 2022

Community Treasure Hunt

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

Start Hunting!

Translated by