LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Very basic question in Matlab (https://www.linuxquestions.org/questions/programming-9/very-basic-question-in-matlab-4175440190/)

andiemac 12-05-2012 04:31 PM

Very basic question in Matlab
 
Let me start by saying this is probably going to be super basic for most of you. My goal is to make GUI that displays multiple questions separately in a random order using randperm. I've got my initial question popping up just fine, but I'm not sure how to use randperm for automatically moving on to the next random question, at all. The only time I've used randperm is working with numbers in a matrix. One of my question's codes is below:
function Noteven()

f = figure


handles.questionOne = uicontrol('style','text','position',[400 200 150 60], ...
'string', 'Which number is not even?', 'fontsize', 16);%

uicontrol('style','popupmenu','string','Choose|3|2|1')
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');

end

Sergei Steshenko 12-06-2012 03:46 AM

Quote:

Originally Posted by andiemac (Post 4843458)
Let me start by saying this is probably going to be super basic for most of you. My goal is to make GUI that displays multiple questions separately in a random order using randperm. I've got my initial question popping up just fine, but I'm not sure how to use randperm for automatically moving on to the next random question, at all. The only time I've used randperm is working with numbers in a matrix. One of my question's codes is below:
function Noteven()

f = figure


handles.questionOne = uicontrol('style','text','position',[400 200 150 60], ...
'string', 'Which number is not even?', 'fontsize', 16);%

uicontrol('style','popupmenu','string','Choose|3|2|1')
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');

end


I don't know Matlab, but I know GNU Octave pretty well. It doesn't have a GUI though, but I think GUI issues is not you problem, you problem is:
"I'm not sure how to use randperm for automatically moving on to the next random question, at all".

If my understanding is correct, your question is not Matlab-specific, it's general programming question. The solution is along the following lines:

1) you create a cell array with your set of questions:

Code:

questions = {"goo ?", "bar ?", "d\'oh !?", "dah ?", "who ?", "what ?"};
So, you have 'numel(questions)' possibilities.

Then you use any random number generation function, e.g. in Octave in may case it's

Code:

`random' is a function from the file /mnt/sdb8/sergei/AFSWD_debug/20121021/octave-3.6.2/share/octave/packages/statistics-1.1.3/random.m
and condition it to generate numbers in in the 1 .. numel(questions) range.

Then you use the generated random number as index to 'questions' cell array.

johnsfine 12-06-2012 07:16 AM

Quote:

Originally Posted by Sergei Steshenko (Post 4843667)
I don't know Matlab, but I know GNU Octave pretty well.

I know very little of either, but ...

Quote:

If my understanding is correct, your question is not Matlab-specific, it's general programming question.
Except that in Matlab it is usually easier to use arrays for situations where in most programming languages it would be easier to use switch statements and iteration etc.

Quote:

1) you create a cell array with your set of questions:
Sounds like the right idea (though on the details, you're already past my very limited Matlab knowledge).

If I understood the OP, each "question" has been constructed as a separate function that must be called to ask the question. The question is not represented as simply some text.
Andiemac, please correct me if I misunderstood that.

If I'm right so far, that array of questions must be an array of something that acts like function pointers. I have no clue how to do that in Matlab, but I expect it can be done and I expect it is the same in Octave.

Quote:

Then you use any random number generation function, e.g. in Octave in may case it's
No. There the OP was already on the right path. A random permutation is required, not a sequence of independent random selections. Since I didn't read the first post carefully enough, I googled "matlab random permutation" and found the same thing the OP mentioned:
http://www.mathworks.com/help/matlab/ref/randperm.html

Maybe a glance at that will help you improve your answer.

Quote:

Then you use the generated random number as index to 'questions' cell array.
So we assume an array Q of questions, and an array (generated by randperm) of indexes into Q. The OP's question comes down to how to iterate over the index array invoking the questions in that sequence.

If Matlab doesn't support something that acts like an array of function pointers, then you probably need to do that the crude way with a switch statement.

Edit: A little help from google told me that Matlab function pointers are called function handles and the way to work with an array of them is documented at the end of the page at
http://www.mathworks.com/help/matlab...on_handle.html

Sergei Steshenko 12-06-2012 07:38 AM

Quote:

Originally Posted by johnsfine (Post 4843743)
... A random permutation is required, not a sequence of independent random selections. ...

If each question is required to be asked just once, then yes. If questions can be asked several times, then randomly generated index is OK.


All times are GMT -5. The time now is 03:45 AM.