Luis
Luis

Reputation: 11

How do you create a floating point array in MATLAB?

I'm trying to create a 10 x 8 array in MATLAB filled with floating-point values.

Ideas?

UPDATE:
I am actually trying to create an empty 10 x 8 float-type array. How can I do that?

Upvotes: 1

Views: 12427

Answers (4)

Lynlyn
Lynlyn

Reputation: 1

This is an old question but I'm posting my answer in case anyone stumbled across the same problem. Assuming that you're trying to get 32 digits precision, use M=vpa(zeros(10,8)).

Upvotes: 0

MartinStettner
MartinStettner

Reputation: 29164

You might want to have a look at the zeros function. To create a 10 x 8 matrix containing all zeros, use

matrix = zeros(10, 8);

To force the elements to be of a certain type (e.g. single precision), use the additional class argument like

matrix = zeros(10, 8, 'single');

(I think, the default is double precision)

Upvotes: 3

Barney Szabolcs
Barney Szabolcs

Reputation: 12514

UPDATE: the clarification from the OP made this answer outdated.


If you just want to create a matrix with specific values, here is a one-liner approach:

data = [0. 0.1 0.2 3. 4. 5. 6. 7. 8. 0.9;0. 0.1 0.2 3. 4. 5. 6. 7. 8. 0.9; ...; 0. 0.1 0.2 3. 4. 5. 6. 7. 8. 0.9]

multi-liner approach (if you are about to copy-paste data):

data = [
0. 0.1 0.2 3. 4. 5. 6. 7. 8. 0.9
0. 0.1 0.2 3. 4. 5. 6. 7. 8. 0.9
0. 0.1 0.2 3. 4. 5. 6. 7. 8. 0.9
0. 0.1 0.2 3. 4. 5. 6. 7. 8. 0.9
...
0. 0.1 0.2 3. 4. 5. 6. 7. 8. 0.9
]

However, as many wrote rand(10,8) right off the bed, you can see, it is common practice not to use some kind of function to create a (10,8) matrix. Say: rand,ones, zeros, or some other tricks say reshape((1:1:80), 10, 8).

Upvotes: 2

doctor killer
doctor killer

Reputation: 392

matrix = single(rand(10,8));

float is a single in Matlab

rand(10,8); returns a matrix of dimension 10x8 formatted as doubles...you can cast the return value to single(rand(10,8)) to get floating point values...if for some reason you need to have floating point precision instead of double floating point procision

Upvotes: 2

Related Questions