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

6 次查看(过去 30 天)
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 个评论
Rebeca Miyar
Rebeca Miyar 2018-8-15
It worked now, just had a problem with one of the variables, thank you very much for your help!
Stephen23
Stephen23 2018-8-15
编辑:Stephen23 2018-8-15
@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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by