How can I flip an array without the flip function?

5 visualizzazioni (ultimi 30 giorni)
Sarah
Sarah il 7 Mar 2018
Commentato: Jan il 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 Commento
Jan
Jan il 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.

Accedi per commentare.

Risposte (1)

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

Community Treasure Hunt

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

Start Hunting!

Translated by