%How can i create this vector? Please I need your help.

Vector = [1 1 1 1 1 0 0 2 2 2 2 2 0 0 3 3 3 3 3 ...];

2 Kommentare

What pattern do you want? I see 1 block of 1s, 3 blocks of 2s, so you want 5/6 blocks of 3s?
Sorry, it was a mistake. Thanks

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Maryam  John
Maryam John am 9 Mai 2018
Bearbeitet: Maryam John am 21 Mai 2018
B = 1:50;
Vector = repelem(B, 7);
D = 0;
for j = 1:size(Vector)
D = D +1;
if (mod(D, 7) == 0 || mod(D, 7)==6)
Vector(j) = 0;
end
end

2 Kommentare

Stephen23
Stephen23 am 9 Mai 2018
Bearbeitet: Stephen23 am 9 Mai 2018
Simpler:
V = repelem(1:50,7);
V(6:7:end) = 0;
V(7:7:end) = 0;
Some notes about your answer:
  • Transposing the vector on every single loop iteration serves no purpose.
  • The variable D just replicates the value of j, so is superfluous.
  • linspace(1, A, A) is just 1:A.
Thanks for the comment

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Stephen23
Stephen23 am 9 Mai 2018
Bearbeitet: Stephen23 am 9 Mai 2018
Simple use of repelem and basic MATLAB indexing:
V = repelem(1:50,7);
V(6:7:end) = 0;
V(7:7:end) = 0;
Or for older MATLAB versions:
V = repmat(1:50,7,1);
V(6:7,:) = 0;
V = reshape(V,1,[]);

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by