create an array with general rules

조회 수: 2 (최근 30일)
Shan  Chu
Shan Chu 2016년 5월 18일
댓글: Shan Chu 2016년 5월 18일
Dear all, I am struggling with Matlab, please help me. I want to create an array with some certain rules. Suppose I have an array [1,n]
a=1
b=6
c=4
I1=1
I2=2
I3=3
The rules are the number of I1 in the array is a, the number of I2 is b, the number of I3 is c. So the array should be like:
I=[I1 I2 ... I2 (b elements) I3...I3 (c elements)];
Please don't suggest me to list all the elements in 1 command. This is just an example to illustrate the problem. Thanks
  댓글 수: 1
Adam
Adam 2016년 5월 18일
편집: Adam 2016년 5월 18일
doc repmat
should help with that.
Or something like
b * ones(1,I2);
etc, though for an extensible solution it would need more thought, but having individual variables named I1, I2, I3 suggests it isn't extensible anyway so if that is the limit of how far you want to go then repmat or ones should work fine.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 5월 18일
For versions before that use repmat()

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by