MATLAB: Select a random number from each row of a matrix

randomselect numbers

Hi Everyone I want to select a random number of every row of a matrix like this one: A=[1 2 3;8 7 2;5 8 1,…]. How can I do that?please help me with.Tnx

Best Answer

Try this:
A=[1 2 3; 8 7 2; 5 8 1];
cidx = randi(3, size(A,1), 1); % Column Indices
Out = A(sub2ind(size(A), (1:size(A,1))', cidx)); % Create Linear Indices & Return Appropriate Elements
Related Question