How can I flip an array without the flip function?

4 visualizaciones (últimos 30 días)
Sarah
Sarah el 7 de Mzo. de 2018
Comentada: Jan el 7 de Mzo. de 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 comentario
Jan
Jan el 7 de Mzo. de 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.

Iniciar sesión para comentar.

Respuestas (1)

KSSV
KSSV el 7 de Mzo. de 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

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by