0
votes

I'd like get k random subscripts from A to be used in a C++ program with currently load data from a file given a set of subscripts.

I've a MxN matrix A with double values and a Mx1 matrix B with integers from 0 to 20.

How could i get k random subscripts from A with a condition from B ? In example, given:

A = [ 0.25 0.25 0.25 0.25
      0.18 0.18 0.18 0.18 
      0.36 0.36 0.36 0.36 
      0.51 0.51 0.51 0.51 ]

B = [ 0 
      1
      2
      1 ]

I'm trying to get k = 1 random row subscript i from A if B(i) == 1. So, I'm looking for i == 1 or i == 4.

I've tried first create a logical index as:

idx = B == 1;

And then, get from A the elements with that condition as follow:

r = A( idx, : );

And finally, do a permutation in r to in order to get k rows:

randperm( size(r) )

But I'm stuck now as I do not know how to translate the permutation to the matrix A.

I'm also trying to understand the function [I,J] = ind2sub(siz,IND) but no idea right now how to join the subscripts with the randon permutation. Moreover, the results of randperm( size(r) ) are related to the size of r.

So, How could i get k random subscripts from A with a condition from B ? The idea is use the subscripts in a C++ program as input parameters

1
I don't understand what you're trying to do. It looks like you use B to select the rows of A you are interested in, but then what? Do you want a random element from those rows of A? Please try to explain some more. - shoelzer
@shoelzer The idea is use the subscripts in a C++ program as input parameters - pQB

1 Answers

1
votes

You can get the relevant row numbers as follows:

nrs=1:length(idx);
nrs=nrs(idx);

Now to permute them:

nrs_permidx = randperm(length(nrs))
permuted = nrs(nrs_permidx)

And I guess you can take it from here.