필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
Rebeca Miyar
Rebeca Miyar 2018년 8월 15일
편집: Stephen23 2018년 8월 15일
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