next up previous contents
Next: FIND Find Non-zero Elements Up: Array Generation and Manipulations Previous: ZEROS Array of Zeros   Contents

Subsections

ONES Array of Ones

Usage

Creates an array of ones of the specified size. Two seperate syntaxes are possible. The first syntax specifies the array dimensions as a sequence of scalar dimensions:

   y = ones(d1,d2,...,dn).

The resulting array has the given dimensions, and is filled with all ones. The type of y is float, a 32-bit floating point array. To get arrays of other types, use the typecast functions (e.g., uint8, int8, etc.).

The second syntax specifies the array dimensions as a vector, where each element in the vector specifies a dimension length:

   y = ones([d1,d2,...,dn]).

This syntax is more convenient for calling ones using a variable for the argument. In both cases, specifying only one dimension results in a square matrix output.

Example

The following examples demonstrate generation of some arrays of ones using the first form.

--> ones(2,3,2)
ans = 
  <float>  - size: [2 3 2]
(:,:,1) = 
 
Columns 1 to 3
    1.0000000          1.0000000          1.0000000       
    1.0000000          1.0000000          1.0000000       
(:,:,2) = 
 
Columns 1 to 3
    1.0000000          1.0000000          1.0000000       
    1.0000000          1.0000000          1.0000000       
--> ones(1,3)
ans = 
  <float>  - size: [1 3]
 
Columns 1 to 3
    1.0000000          1.0000000          1.0000000

The same expressions, using the second form.

--> ones([2,6])
ans = 
  <float>  - size: [2 6]
 
Columns 1 to 3
    1.0000000          1.0000000          1.0000000       
    1.0000000          1.0000000          1.0000000       
 
Columns 4 to 6
    1.0000000          1.0000000          1.0000000       
    1.0000000          1.0000000          1.0000000       
--> ones([1,3])
ans = 
  <float>  - size: [1 3]
 
Columns 1 to 3
    1.0000000          1.0000000          1.0000000

Finally, an example of using the type casting function uint16 to generate an array of 16-bit unsigned integers with a value of 1.

--> uint16(ones(3))
ans = 
  <uint16>  - size: [3 3]
 
Columns 1 to 3
     1      1      1  
     1      1      1  
     1      1      1


next up previous contents
Next: FIND Find Non-zero Elements Up: Array Generation and Manipulations Previous: ZEROS Array of Zeros   Contents
Samit K. Basu 2005-03-16