Effacer les filtres
Effacer les filtres

How can I create a 1X250 array with every 50 numbers being the same?

6 vues (au cours des 30 derniers jours)
Rebeca Miyar
Rebeca Miyar le 15 Août 2018
Modifié(e) : Stephen23 le 15 Août 2018
I'm trying to create a matrix in matlab that is 1X250, that every 50 numbers are the same. It needs to look something like this: [111222333...] I tried the following code:
E=[zeros(1,NOE)];
E(1:50)=E_1;
E(51:100)=E_2;
E(101:150)=E_3;
E(151:200)=E_4;
E(201:250)=E_5;
While E_1/2/3/4/5 are num. that I defined earlier in program. I keep getting an error, what can I do?
  5 commentaires
Rebeca Miyar
Rebeca Miyar le 15 Août 2018
It worked now, just had a problem with one of the variables, thank you very much for your help!
Stephen23
Stephen23 le 15 Août 2018
Modifié(e) : Stephen23 le 15 Août 2018
@Rebeca Miyar: note that these square brackets do nothing:
E=[zeros(1,NOE)];
Square brackets are a concatenation operator, and you are not concatenating anything together with that code: zeros already returns a vector, so the square brackets do nothing whatsoever, except perhaps slowing your code down and making it more complex.
Using numbered variables is a sign that you are doing something wrong. If those variables are numbered and belong in a sequence, then they should be stored in one variable. This will make your code simpler and more efficient. For example, if you stored those value in one vector then you could have simply done this:
vec = [1,2,3,4,5]
new = repelem(vec,50)
or for versions without repelem:
new = reshape(repmat(vec,50,1),1,[])
Simpler, better data design leads to simpler, better, more efficient code.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by