Create matrix with random binary element in matlab

前端 未结 5 872
情歌与酒
情歌与酒 2021-01-11 16:57

I want to create a matrix in matlab with 500 cell (50 row,10 column ), How I can create and initialize it by random binary digits? I want some thing like this in 50*10 scal

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-11 17:41

    Try this:

    A = rand(50, 10) > 0.5;
    

    The decimal equivalent of the nth row is given by:

     bin2dec(num2str(A(n,:)))
    

    or, if you prefer,

    sum( A(n,:) .* 2.^(size(A,2)-1:-1:0) )   % for big endian
    sum( A(n,:) .* 2.^(0:size(A,2)-1) )      % for little endian
    

    which is several times faster than bin2dec.

提交回复
热议问题