Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Selecting randomly a number from a matrix within a given range

Subject: Selecting randomly a number from a matrix within a given range

From: Diego Zegarra

Date: 20 Oct, 2008 04:31:24

Message: 1 of 8

Alright I have a problem selecting randomly a number that is within a range. I have m matrices of nxn as MTT{m},and if we look at the min value of the diagonals it is 52 and the max value is 97. So now I want to select randomly a number from the diagonals that is within 52 and 52 + (97-52)*0.4 = 70. This means that in the first matrix MTT{1} I can select from 52,59 and 65. Now in the second matrix we would have 63 and 57.
So I want to be able to select randomly one of those 5 values and know from what matrix it was from MTT{m} and also the value and its position (column number) within the matrix.

52 92 92 69 68 53
90 84 63 91 59 71
77 80 71 94 65 61
54 54 60 86 54 99
64 64 98 65 59 70
87 59 61 61 96 65

87 73 55 58 67 55
84 97 51 62 98 89
90 54 63 68 68 65
52 77 81 74 85 54
95 56 96 71 57 92
70 58 59 95 66 78

Also I would like it that everytime I do this it does not select the same value. I want the random generator to start randomly not always the same.

I hope I made myself clear and thanks in advance for your help!

Subject: Selecting randomly a number from a matrix within a given range

From: Walter Roberson

Date: 20 Oct, 2008 05:35:32

Message: 2 of 8

Diego Zegarra wrote:
> Alright I have a problem selecting randomly a number that is within a range. I have
> m matrices of nxn as MTT{m},and if we look at the min value of the diagonals it is 52
> and the max value is 97. So now I want to select randomly a number from the diagonals
> that is within 52 and 52 + (97-52)*0.4 = 70.

First step:

diags = cell2mat( cellfun(@diag, MTT) );
dimin = min(diags(:));
dimax = max(diags(:));
rmin = dimin;
rmax = dimin + floor( 0.4 * (dimax - dimin) );
eligibleentry = (diags >= rmin) & (diags <= dimax);
eligibleidxs = find(eligibleentry(:));
numeligible = length(eligibleidxs);

> This means that in the first matrix MTT{1}
> I can select from 52,59 and 65. Now in the second matrix we would have 63 and 57.
> So I want to be able to select randomly one of those 5 values and know from what matrix
> it was from MTT{m} and also the value and its position (column number) within the matrix.

> Also I would like it that everytime I do this it does not select the same value. I want the
> random generator to start randomly not always the same.

That part about "does not select the same value" is not completely clear. If
the selection procedure -happened- to select 52 three times in a row, and then
63, and then 59, and then 52 again twice before going on to other numbers, would
that be acceptable, on the grounds that it was not always selecting the same value?
If so, then rand() would be fine for that, provided that there was more than one
eligible entry. Unless, that is, what you mean is that you need the order to be
different between different matlab sessions: normally rand() will use the same
random order for each matlab session; if you need different matlab sessions to use
different orders, then see the documentation in rand() for how to set the seed.

Or do you need it to "select without replacement" -- that is, you need it to pick
one of the eligible entries each round, but not one that has already been picked
before? That is what I am going to assume for the below:

randorder = randperm(numeligible);
randidxs = eligibleidxs(randorder);
valsinorder = diags(randidx);
[matrixnums, columnnums] = ind2sub(size(diags), randidxs);

Then the complete list is:

valsinorder(K) -- the K'th randomly selected value
matrixnums(K) -- which matrix the K'th value was from
columnnums(K) -- which column within the matrix the K'th value was from


You can see that this sequence does all of the "selection without replacement"
and all of the matrix and column number location without using any loops.

Note: this logic relies upon the candidates for the random pool being
exactly one fixed item per column, and relies upon each matrix having
the same size. The logic could be extended fairly readily to the case
where the matrices might have different sizes, but the index
calculations would get messier if there was more than one eligible
member per column. If the random selection could be from anywhere
in the matrices and if the matrices were different sizes, it would
probably be easier to write new logic.

Subject: Selecting randomly a number from a matrix within a given range

From: Diego Zegarra

Date: 20 Oct, 2008 14:10:03

Message: 3 of 8

Walter thanks for your reply but it does not seem to be working. It throws me this error message. Could anyone help me with this??

diags = cell2mat( cellfun(@diag, CTT) )
??? Error using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.

Also, what I meant that it is different everytime is that every iteration I do for this problem, it will select a number randomly in a random order, no every iteration select the same value over and over again.

Subject: Selecting randomly a number from a matrix within a given range

From: Diego Zegarra

Date: 20 Oct, 2008 14:28:03

Message: 4 of 8

Ok nevermind with my last post as I fixed that already. I had to change the code to be,

diags = cell2mat(cellfun(@diag,CTT, 'UniformOutput',false))

Now ok this code given earlier by Walter does get the values within a range and everything but HOW to select one of those randomly and get the matrix where it is from and the column number?

Thanks again in advance for ANYONE's help!

Subject: Selecting randomly a number from a matrix within a given range

From: Diego Zegarra

Date: 20 Oct, 2008 17:40:18

Message: 5 of 8

Could anyone please help me with this? Any hints on how I can do this? I need it soon! Thanks

Subject: Selecting randomly a number from a matrix within a given range

From: dpb

Date: 20 Oct, 2008 17:42:42

Message: 6 of 8

Diego Zegarra wrote:
> Ok nevermind with my last post as I fixed that already. I had to change the code to be,
>
> diags = cell2mat(cellfun(@diag,CTT, 'UniformOutput',false))
>
> Now ok this code given earlier by Walter does get the values within a
>
range and everything but HOW to select one of those randomly and get the
matrix where it is from and the column number?

What was/is wrong w/ Walter's solution he provided?

--

Subject: Selecting randomly a number from a matrix within a given range

From: Diego Zegarra

Date: 20 Oct, 2008 18:46:02

Message: 7 of 8

That it gives me the indexes for the values that satisfy but I dont know from what machine those indexes are or the indexes of that value selected within that machine.

Hope I made myself clear.

Thanks

Subject: Selecting randomly a number from a matrix within a given range

From: Walter Roberson

Date: 20 Oct, 2008 19:48:21

Message: 8 of 8

Diego Zegarra wrote:
> That it gives me the indexes for the values that satisfy but I dont know from what
> machine those indexes are or the indexes of that value selected within that machine.

We don't know either. Your question did not mention "machines" so my code did not attempt
to account for them.

If a "machine" corresponds to a matrix, then like I said in my summary:

  Then the complete list is:

  valsinorder(K) -- the K'th randomly selected value
  matrixnums(K) -- which matrix the K'th value was from
  columnnums(K) -- which column within the matrix the K'th value was from


To be more concrete,

valsinorder(1) is the first random value that was selected, and that value
was selected from matrix (machine?) matrixnums(1), and the column that was
selected within that matrix was columnnums(1).

valsinorder(2) is the second random value that was selected, and that value
was selected from matrix (machine?) matrixnums(2), and the column that was
selected within that matrix was columnnums(2).

and so on.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us