Using randperm to generate a shuffled cell array. How do i get the original ordered cell array

7 ビュー (過去 30 日間)
I try to create a trivia-game where the user have to enter F for a false question and T for a true question.
I have a cell array Q containing several strings of questions. To sort Q randomly, I do this:
neworder = randperm(numel(Q));
Qshuffled = Q(neworder)
Using a while-loop I create a cell array R containing -1 for wrong answers given by the user, 0 if no answers is given and 1 for correct answers. Now (naturally) the order of answers in R corresponds to the order of questions in Qshuffled. I now want to re-order R to the same order as Q, so that the answers given by the user can be compared to the original order of questions in Q.
I hope someone can help me. Thanks.
If it helps the While-loop looks like this (A is a logical vector containing 1 for true answer and 0 for false answer)
neworder = randperm(numel(Q));
Qshuffled = Q(neworder);
A = A(neworder);
while run_random == 1 && i <= numofQ
tic
userinput = input([Qshuffled{i} ' '], 's');
switch userinput
case 'T'
if isequal(1, A(i))
R(i) = 1;
else
R(i) = -1;
end
case 'F'
if isequal(0, A(i))
R(i) = 1;
else
R(i) = -1;
end
case ''
R(i) = 0;
case 'Afslut'
break
otherwise
display(' ')
display('Dit input er ugyldigt.')
display (' ')
pause(1.5)
display('Tast:')
display(' ''T'' for sandt udsagn.')
display(' ''F'' for falsk udsagn.')
display(' ''Enter-knappen'', hvis du ikke kender svaret.')
display(' ''Afslut'' hvis du ønsker at forlade testen.')
display(' ')
Askagain = 1;
end
O{i} = Qshuffled{i};
T(i) = toc;
if Askagain == 0
i = i + 1;
else
Askagain = 0;
end
end

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 2 日
Example
a={'a' 'b' 'c' 'd' 'e' 'f'}
n=numel(a)
ii=randperm(n)
[~,previous_order]=sort(ii)
b=a(ii)
%to get a
old_a=b(previous_order)
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 2 日
For example
[a,b]=sort([3 2 5])
% a is a sorted array
% b is the corresponding inf=dices
If you don't need a, just write
[~,b]=sort([3 2 5])
Kenneth
Kenneth 2014 年 5 月 2 日
oh, i see.
Again, thanks a lot

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by