How can I create a 1X250 array with every 50 numbers being the same?
4 次查看(过去 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 个评论
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 Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!