MATLAB: How to choose a random value from an array

arrayMATLAB

Hi,
I have an array of values and I need to frame an expression such that it chooses the value from the array automatically instead of me assigning a value from the array everytime. For example, if
A_index=[1 2 3 4]
B_index=[5 6 7 8]
while initialising A, B for the first time I choose A=2, and B=6 (A and B are selected with corresponding element) and next time A=4, B=8
However, I have used this coding
A=@(A_index) A_index(ceil(rand*length(A_index)))
B=@(B_index) B_index(ceil(rand*length(B_index)))
But this line doesn't generate a value, instead it returns the expression itself. Why is that so?
Could someone please help me.

Best Answer

Then you must want to only generate a single index instead of two and use it for both--
idx=randperm(length(A_index),1);
A=A_index(idx);
B=B_index(idx);