Tim
Tim

Reputation: 145

Generate matrix of random number with constraints in matlab

I want to generate a matrix of random numbers (normrnd with mean == 0) that satisfy the following constraints using MATLAB (or any other language)

  1. The sum of the absolute values in the matrix must equal X
  2. The largest abs(single number) must equal Y
  3. The difference between the number and its 8 neighbors (3 if in corner, 5 if on edge) must be less than Z

It would be relatively easy to satisfy one of the constraints, but I can't think of an algorithm that satisfies all of them...

Any ideas?

I am not sure whether to edit my post or to reply here, so I am editing... @MZimmerman6, you have a point. Though these constraints won't produce a unique solution, how would I obtain multiple solutions without using rand?

A very simply 3 x 3 where 5 is the max element value, 30 is the sum, and 2 is the difference
5 4 3
4 4 2
3 2 3

Rody, that actually may help...I need to think more :)

Luis ...Hmmm...why not? I can add up the absolute value of a normally distributed sample...right?

Upvotes: 2

Views: 1009

Answers (1)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21561

Here is an algorithm to get the 'random' numbers that you need.

  1. Generate a valid number (for example in the middle)
  2. Determine the feasible range for one of the numbers next to it
  3. If there is no range, you go to step 1, otherwise generate a number and continue

Depending on your constraints it may take a while of course. You could add an other step to see if changing the existing numbers would help before going back to step 1.

Upvotes: 1

Related Questions