How can I flip an array without the flip function?

3 Ansichten (letzte 30 Tage)
Sarah
Sarah am 7 Mär. 2018
Kommentiert: Jan am 7 Mär. 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 Kommentar
Jan
Jan am 7 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

KSSV
KSSV am 7 Mär. 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

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by