How to I reverse the contents of this array?

62 次查看(过去 30 天)
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);

类别

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