MATLAB: Returning index values (elements) of a randomly generated matrix (using ceil command) in Matlab

ceilindex valuesMATLABrandomrandom number generator

Hi
I have a matrix/vector that has 100 randomly generated values of 0 or 1 as:
matrix_nodes = ceil( rand(1,number_nodes)*2)-1
Now, from the above matrix/vector, I want two matrices/vectors such that I get all values (elements) which are 1 and 0 from matrix_n respectively:
matrix_ones = find(matrix_nodes==1)
matrix_zeros = find(matrix_nodes==0)
  • But the above code only return the indices of all the ones in matrix_n. *
Can anyone help me how to get values of all the ones from matrix_n in matrix_ones and values of all zeros from matrix_n in matrix_zeros.

Best Answer

matrix_ones =matrix_nodes(matrix_nodes==1)
matrix_zeros = matrix_nodes(matrix_nodes==0)