Clear Filters
Clear Filters

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

6 views (last 30 days)
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 Comments
Rebeca Miyar
Rebeca Miyar on 15 Aug 2018
It worked now, just had a problem with one of the variables, thank you very much for your help!
Stephen23
Stephen23 on 15 Aug 2018
Edited: Stephen23 on 15 Aug 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.

Sign in to comment.

Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!