dimanche 31 mai 2020

Octave: Filling specific values in random cells inside a table

I am trying to fill some values in a table (1 row n columns). 1 and 0 must be inside the table but in random positions and the remaining cells have to be random values between 0 and 1.

e.g for n=4 P should look something like this

P = 0.0000 0.58177 1.00000 0.86008

What I have done is: filling the first two positions inside the table with 1 and 0 and the rest of n (if n>2) is filled with randomized values [0,1]. Any ideas how to fix my code are welcome.

function P=pairwise_matrix(n)

P=zeros(1,n);

 a = 0; b = 1;
 x = a + (b-a) .* rand(1,1);



  P(1,1)=1

  if (n==1)
    printf("not applicable")
  endif

  if (n==2)
    P(1,1)=0
    P(1,2)=1
  endif


  for (i=3:n)
        P(1,i)=rand(x);
  endfor




endfunction



Aucun commentaire:

Enregistrer un commentaire