How to specify a number to the specified position in the array?

조회 수: 3 (최근 30일)
YANGCHENG LIUL
YANGCHENG LIUL 2019년 10월 13일
답변: Andrei Bobrov 2019년 10월 14일
matlab问题图.jpgHi guys, the question is that now I used 'zeros' command, to create an array which have 20 positions, then there are some data will be created to fill the array, and I want to point data to the specified position. For example, the No.1 data should stay in the first position, the end data should stay in the second position, the No.2 data should stay in the third position. Is there any function can help me arrange their positions? Thanks a lot !

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 10월 14일
a = [2 3 5 7 11];
out = [a;flip(a,2)];
out = out(1:numel(a));

추가 답변 (1개)

the cyclist
the cyclist 2019년 10월 13일
편집: the cyclist 2019년 10월 13일
There's probably not an built-in function that does just that, but it's easy to do it with basic array manipulations. Here is one way.
newData = 1:20; % Your example "new" data
newNewData = [newData(1:10); newData(20:-1:11)]; % Make even newer data, temporarily in 2-d array
newNewData = newNewData(:)' % Make the newer data into a row array
  댓글 수: 5
the cyclist
the cyclist 2019년 10월 14일
편집: the cyclist 2019년 10월 14일
When you say "random", do you mean the length of the vector is random? Or that the values in the vector are random?
If your data is
[2 3 5 7 11]
what would you want the output to be?
YANGCHENG LIUL
YANGCHENG LIUL 2019년 10월 14일
random length of vector and random data, it is a length calculation function, the number of variable maybe 10, 15, or 20, and the value is random too. As you gaved example, I want the data can be arranged as [2 11 3 7 5].

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

카테고리

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