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])

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by