Azzera filtri
Azzera filtri

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]?

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
John D'Errico
John D'Errico il 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 il 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.

Accedi per commentare.

Risposte (2)

William Rose
William Rose il 6 Nov 2023
Modificato: William Rose il 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.

John D'Errico
John D'Errico il 6 Nov 2023
Modificato: John D'Errico il 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.

Categorie

Scopri di più su Historical Contests in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by