Effacer les filtres
Effacer les filtres

How can I make a vector that looks like this [2 1 2 1 1 4 1 1 6 1 1 8...1 100 1]?

1 vue (au cours des 30 derniers jours)
Rowdy
Rowdy le 6 Nov 2023
Commenté : William Rose le 7 Nov 2023
I am attempting to make a 151 element vector that starts out like [2 1 2 1 1 4...] so that between the 3n elements in the vector after the initial 3 values of 2, 1, 2 there are 2 ones followed by a term that is 2 more than the value of the 3rd element in the vector i.e (2 1 1 4 1 1 6 1 1 8 1 1 10 1 1... ) all the way to 1 1 100 1 as the last 4 elements in the vector.
cv4 = [2 1 2 ones(1,148)];
for i = 1:75
cv4(:,2*(i+1)) = 2;
end
cv4
This is the code i've used so far but it isn't anywhere near right.
cv4 = 1×152
This is the vector I have so far... [2 1 2 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2]
  2 commentaires
John D'Errico
John D'Errico le 6 Nov 2023
Your question is too confusing. Is the vector of length 151? Or 152?
What are the fist elements? In two places, I have seen it start out as
2 1 1 4 1 1 6 1 1 8 1 1 10
Or maybe this
2 1 2 1 1 4 1 1 6 1 1 8
So what do you want to do? Can we answer a question that is not even consistent?
Rowdy
Rowdy le 6 Nov 2023
It starts out as 2 1 2 and then begins the repition of 2 1 1 4 1 1 6 and so on and so forth until the last 4 terms of the vector are 1 1 100 1. The lengrth of the vector is 151 elements it is just that my code is wrong. My apologies.

Connectez-vous pour commenter.

Réponses (2)

William Rose
William Rose le 6 Nov 2023
Modifié(e) : William Rose le 6 Nov 2023
cv4=[2,1,2,ones(1,148)];
for i=6:3:151, cv4(i)=2*i/3; end
cv4
cv4 = 1×151
2 1 2 1 1 4 1 1 6 1 1 8 1 1 10 1 1 12 1 1 14 1 1 16 1 1 18 1 1 20
Try it.
  2 commentaires
Rowdy
Rowdy le 6 Nov 2023
Modifié(e) : Rowdy le 6 Nov 2023
Wow thank you for the insight! Thank you very much for the help! It worked and now I am able to implement this.

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 6 Nov 2023
Modifié(e) : John D'Errico le 6 Nov 2023
If I had to guess, you might want to do this:
V = ones(1,151);
V(1) = 2;
V(3:3:end) = 2:2:100;
V
V = 1×151
2 1 2 1 1 4 1 1 6 1 1 8 1 1 10 1 1 12 1 1 14 1 1 16 1 1 18 1 1 20
The last few elements of the vector are:
V(140:151)
ans = 1×12
1 94 1 1 96 1 1 98 1 1 100 1
But your question is far too confusing to know if that is what you want or not.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by