MATLAB: How do i select a random cell with a certain value from the matrix randomselect value Hello, I have a 1024×1280 matrix that only has the values 0 and 1. Now I want to randomly select a cell that has the value 1. Anybody got an idea how to do that? Thanks Best Answer % Create matrix of 0s & 1sM = round(rand(1024, 1280)); % List all indices of 1sloc = find(M==1); % Randomly select one of the indicesrandInd = datasample(loc, 1); % or randInd = loc(randi(numel(loc)))% Show the row and col number of the selection[row, col] = ind2sub(size(M), randInd); Related SolutionsMATLAB: Outputting numbers not selected in using “randperm” command Select_How_Many = 42;Num_in_Row = length(row);selection_index = randperm(Num_in_Row, Select_How_Many);unselection_index = setdiff(1:Num_in_Row, selection_index);selected_values = row(selection_index);unselected_values = row(unselection_index); MATLAB: Line and column of the minimun element of a matrix m = [5 1 -400; ... -6 100 -6; 2 5 25];[~, ind] = min(m(:));[row, col] = ind2sub(size(m), ind)Or:[v, ind1] = min(m, [], 1); % Along 1st dimension[~, col] = min(v, [], 2); % Along 2nd dimensionrow = ind1(col) Related QuestionHow to select one of the array and change the array data selected with another valueHow to: map selected entries of one matrix to entries of a secondHow to randomly select a row from a matrixExtracting specific values from a MatrixHow to select random number in a matrixGet a set of 10000 unique random 2-D coordinates
Best Answer