flip an array with the use of vectors

조회 수: 1 (최근 30일)
Zenia Askar
Zenia Askar 2020년 1월 12일
댓글: Stephen23 2020년 1월 16일
I would like to flip specific cells in an array, not the whole array, with the use of vectors. For example a=[1 7 5 9 3 2 4 1] and i want to flip it from 5 to 2 and make it a=[1 7 2 3 9 5 4 1]. Any suggestions? Thank you in advance.

채택된 답변

Stephen23
Stephen23 2020년 1월 12일
편집: Stephen23 2020년 1월 12일
"Any suggestions?"
Use indexing (which in MATLAB starts from 1):
>> a = [1,7,5,9,3,2,4,1]
a =
1 7 5 9 3 2 4 1
>> a(3:6) = a(6:-1:3)
a =
1 7 2 3 9 5 4 1
  댓글 수: 2
Zenia Askar
Zenia Askar 2020년 1월 16일
Hello again. Eventually, I choose from the array two numbers (not vectors) and I want to flip these cells instead. For example instead of 3rd and 6th cell, I have mumbers 5 and 2. How can i flip the array in this case? Thank you again.
Stephen23
Stephen23 2020년 1월 16일
>> a = [1,7,5,9,3,2,4,1]
a =
1 7 5 9 3 2 4 1
>> a([3,6]) = a([6,3])
a =
1 7 2 9 3 5 4 1

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

추가 답변 (1개)

Meg Noah
Meg Noah 2020년 1월 12일
a=[1 7 5 9 3 2 4 1];
a(3:6) = fliplr(a(3:6));

카테고리

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