How can I flip an array without the flip function?
5 views (last 30 days)
Show older comments
I have an array in which I enter numbers in a certain order, and I need to order them in opposite order: Here's what I have:
n = input('Enter number of integers \n');
% assign to array
for i = 1:n
count(i) = i;
forw(i) = input('Enter an integer ');
reverse(i) = forw(i);
end
% Print headers
fprintf('%5s %5s %5s \n','Count','F','R');
fprintf('__________________ \n');
% Print results
for i = 1:n
fprintf('%5i %5i %5i \n',count(i),forw(i),reverse(i));
end
1 Comment
Jan
on 7 Mar 2018
@Melissa Henry: Please do not delete your question after voluntary helpers spent the time for posting an answer. It is very impolite to make their work useless by removing the contents of the questions. Remember that the nature of the forum is sharing solutions in public, and this is not a cheap way to let your homework be solved.
The question has been restored now.
Answers (1)
KSSV
on 7 Mar 2018
a = rand(5,1) ;
% without loop
iwant1 = a(end:-1:1) ;
% with loop
iwant2 = zeros(size(a)) ;
for i = 1:length(a)
iwant2(i) = a(length(a)+1-i) ;
end
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!