Replace elements in array

조회 수: 26 (최근 30일)
Igor Arkhandeev
Igor Arkhandeev 2021년 1월 20일
댓글: Adam Danz 2021년 1월 20일
I apologize if the question is very stupid. I am quite familiar with the basic functions of matlab, but I do not know such a function. Maybe there isn't one, which is why I'm asking. I'm looking for the best algorithm to do this. Let A be an array of numbers. I have provided an example below. Vector A is not symmetric and needs an algorithm that repeats the lines below. If there is no such thing, then thank you for your attention.
A = [1, 2, 3, 4, 5, 6];
aa = A(2);
A(2) = A(3);
A(3) = aa;

채택된 답변

Adam Danz
Adam Danz 2021년 1월 20일
편집: Adam Danz 2021년 1월 20일
I wouldn't call this an algorithm. It's just indexing. If something more robust is needed we'll need a more detailed description or set of rules to follow.
A = [1, 2, 3, 4, 5, 6];
A = A([1,3,2,4,5,6])
A = 1×6
1 3 2 4 5 6
> Vector A is a vector of unknown length and content, in which the k-th and j-th elements change.
A = [1, 2, 3, 4, 5, 6];
k = 4;
j = 2;
A([k,j]) = A([j,k])
A = 1×6
1 4 3 2 5 6
  댓글 수: 2
Igor Arkhandeev
Igor Arkhandeev 2021년 1월 20일
Thanks for the answer. I will try to describe my problem a little more. Vector A is a vector of unknown length and content, in which the k-th and j-th elements change.
Adam Danz
Adam Danz 2021년 1월 20일
See updated answer.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by