How to specify a number to the specified position in the array?
Show older comments
Hi 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 ! Accepted Answer
More Answers (1)
the cyclist
on 13 Oct 2019
Edited: the cyclist
on 13 Oct 2019
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 Comments
YANGCHENG LIUL
on 13 Oct 2019
Edited: YANGCHENG LIUL
on 13 Oct 2019
the cyclist
on 13 Oct 2019
Did you try to understand what my code is doing, or just blindly copy it?
My code works for the case you wrote in your question. If you need it to be more general, you should write your question in a way that describes the general case.
YANGCHENG LIUL
on 13 Oct 2019
Edited: YANGCHENG LIUL
on 13 Oct 2019
the cyclist
on 14 Oct 2019
Edited: the cyclist
on 14 Oct 2019
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
on 14 Oct 2019
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!