How to I reverse the contents of this array?

조회 수: 61 (최근 30일)
Blair Hall
Blair Hall 2017년 10월 5일
댓글: Steven Lord 2017년 10월 5일
I have it right for one half of the assignment (4 elements) but not the other. The second part that I have wrong is 3 elements.
function mileMarkers = ReverseArray(mileMarkers)
% mileMarkers: Row array of mile marker values
% Reverse the contents of row array mileMarkers
mileMarkers = mileMarkers(end:-1:end-3)
end

채택된 답변

James Tursa
James Tursa 2017년 10월 5일
편집: James Tursa 2017년 10월 5일
For that last index, don't use end-3 since that will only work for a 4 element array. Instead, have that last index be simply 1 which will work for any size array. I.e., mileMarkers(end:-1:1)

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 10월 5일
You can use flipud() or fliplr().
mileMarkers = fliplr(mileMarkers);
  댓글 수: 1
Steven Lord
Steven Lord 2017년 10월 5일
If you're using release R2013b or later, you can use flip.

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

카테고리

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