array indexing select element and change its place in the array.

조회 수: 5 (최근 30일)
mikkel stær
mikkel stær 2021년 3월 14일
댓글: mikkel stær 2021년 3월 14일
hello i am having trouble with indexing methods pls help
how do i select the first element or any specific element and just change its place.
example
if i have an array like this
A=[1,2,3,4,5,6,7,8,9,10]
how do i select the first element, and change its place to either being after the number 5, or at the end.
without having 2 copys of that element.
so they look like this when i am done.
A=[2,3,4,5,1,6,7,8,9,10] or A=[2,3,4,5,6,7,8,9,10,1]
I ALSO NEED THIS FOR VERTICAL ARRAY (i mean 1 columb, and lost of rows) the transposed array of A is now called B
B=A'
i had trouble using indexing, and circshift
i am looking for the most correct way.

채택된 답변

Russel Burgess
Russel Burgess 2021년 3월 14일
I'm not sure this is the most correct way, but you can do this sort of thing with positional indexing, e.g.:
A([2:end 1])
Will move the first element to the end, and:
A([2:5 1 6:end])
Will move the first element to after the 5th element. The same idea works with column vectors.
  댓글 수: 1
mikkel stær
mikkel stær 2021년 3월 14일
thx you seem like a guy that knows MATLAB and it desturbes me that you said, even you dont know, if this is the most correct way. since i looked through the array indexing help page, and noting. but god job this works great, and dont even look like its a kind of hacked together piece of code. so i think it is the most correct way. at least i will be using this short oneliner for the future.
regards mikkel

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by