How can I flip an array without the flip function?

5 vues (au cours des 30 derniers jours)
Sarah
Sarah le 7 Mar 2018
Commenté : Jan le 7 Mar 2018
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 commentaire
Jan
Jan le 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.

Connectez-vous pour commenter.

Réponses (1)

KSSV
KSSV le 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

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by