How do you create a floating point array in MATLAB?

10,969

Solution 1

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)

Solution 2

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).

Solution 3

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

Share:
10,969
Luis
Author by

Luis

Updated on June 27, 2022

Comments

  • Luis
    Luis almost 2 years

    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?

    • MartinStettner
      MartinStettner over 11 years
      So, what have you tried? What values do you want in your matrix? In this form, the question is not really clear ...
    • Barney Szabolcs
      Barney Szabolcs over 11 years
      @MartinStettner I have updated the question for the OP according to what he said in a comment on my answer, hope it is better now.
  • doctor killer
    doctor killer over 11 years
    I don't see why this answer got downvoted...please explain why this would not be the answer and post your's (whoever downvoted this: as if it was not obvious...)
  • doctor killer
    doctor killer over 11 years
    This solution is a very bad coding practice, does not answer to the OP request for a FLOAT matrix and not a double as this solution does. It is not generic and therefore cannot be incorporated as a runtime size-determined matrix...
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    Thx for the explanation. 1st: "floating-point" means either float or double, 2nd: if the OP is about to have an array which is filled explicitly with some values from somewhere else, I would do my multi-liner approach in a quick script. I don't see the "bad coding practice" behind it. I hope this answers your concerns: I have a different angle.
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    Yes, I was the commentless downvoter as I saw you were downvoting my answer without any comments. From my answers' point of view -- as I have explained in the comments -- your answer does not make sense to the same extent, so I leveled up. ;) But let's not fight about non-specific questions. Note that the votes are locked by the system until the next edit.
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    To some extent that is actually the best answer here. :) But who knows what the OP could've meant so I posted an alternative that popped up for me... add sth to your answer, I'll your remove the downvote... ;) I'll be back in an hour or so.
  • MartinStettner
    MartinStettner over 11 years
    Hmm, you should both have spent your downvotes to the question instead of fighting each other :) giving +1 to you and @BarnabasSzabolcs, both answers might be helpful if someone wants to create a MATLAB matrix :) ...
  • doctor killer
    doctor killer over 11 years
    @MatrinStettner : you're right...the question is still not complete
  • Luis
    Luis over 11 years
    The question was because of my frustration at being confronted with some silly message about some utilities pertaining to 'float' structures 'no longer being supported'. But thanks!
  • Luis
    Luis over 11 years
    I didn't necessarily want the array to be filled with values from the outset. It just seems kind of lame if an empty of array of type 'float' can't be made.
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    You might want to add this too in your question so others can have a better understanding.
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    @doctorkiller: Turns out, you won, at the end! ;))
  • MartinStettner
    MartinStettner over 11 years
    You could also use rand(10,8,'single'), which would prevent creating a double matrix and then converting it to single precision...