How can I loop an array from the end to the beginning, with a while loop?

5 ビュー (過去 30 日間)
Sylvie
Sylvie 2012 年 9 月 16 日
Hi,
I have a words array with 6 words in it. Like:
array= { 'A' 'B' 'C' 'D' 'E''F' 'G'} ;
I have to display the words one by one, but from the end to the beginning ( so from G to A), by using a while loop. Up till now I have got the following script:
ArrayLength = numel(wordsArray);
i=ArrayLength;
while (i > 0)
i= i-1;
words= wordsArray(i);
disp(words)
end
This script however only displays the words G to B and I get the following error:
??? Subscript indices must either be real positive integers or logicals.
Error in ==> vraag5 at 16 words= wordsArray(i);
Can anyone please help me out with this?

回答 (1 件)

Wayne King
Wayne King 2012 年 9 月 16 日
ii = length(array);
while (ii>0)
disp(array{ii});
ii = ii-1;
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by