Create matrix with random numbers

J

Jason Bornhoft

I would like to start evaluating Ruby's capabilities in processing large
linear systems and matrices and I would like to know if there is a
simple way to 'randomly' populate various matrices regardless of size or
type.

Any help, comments, anything is greatly appreciated!
 
T

Todd Benson

I would like to start evaluating Ruby's capabilities in processing large
linear systems and matrices and I would like to know if there is a
simple way to 'randomly' populate various matrices regardless of size or
type.

Any help, comments, anything is greatly appreciated!

Not sure what you want exactly, but, maybe...

require 'matrix'
a, b = 2, 3
m = Matrix[*(Array.new(a) {Array.new(b) {rand}})]

Todd
 
J

Jason Bornhoft

Not sure what you want exactly, but, maybe...
require 'matrix'
a, b = 2, 3
m = Matrix[*(Array.new(a) {Array.new(b) {rand}})]

Todd

What I would ideally like is to be able to specify the size of the
matrix and then have a random function populate the matrix with real
integers or, eventually, complex numbers.

Thanks for the guidance...
 
R

Rob Biedenharn

Not sure what you want exactly, but, maybe...

require 'matrix'
a, b = 2, 3
m = Matrix[*(Array.new(a) {Array.new(b) {rand}})]

Todd

What I would ideally like is to be able to specify the size of the
matrix and then have a random function populate the matrix with real
integers or, eventually, complex numbers.

Thanks for the guidance...

So something like:

require 'matrix'
def some_random_function
# make something up, perhaps something complex
end
rows, columns = 2, 3
matrix = Matrix[*(Array.new(rows) {Array.new(columns)
{ some_random_function }})]

And, yes, that just about exactly what you were already given. ;-)

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top