主要内容

Create Arrays with stack and cat Functions

An easy manner to create an array is with stack. Create a [4-by-1] umat array by stacking four 1-by-3 umat objects with the stack command. The first argument of stack specifies in which array dimension the stacking occurs. In the example below, the stacking is done is the 1st array dimension, hence the result is a 1-by-3-by-4-by-1 umat, referred to as a 4-by-1 umat array.

a = ureal('a',4); 
b = ureal('b',2); 
M = stack(1,[a b 1],[-a -b 4+a],[4 5 6],[a 0 0]) 
UMAT: 1 Rows, 3 Columns [array, 4 x 1] 
  a: real, nominal = 4, variability = [-1  1], 1 occurrence 
  b: real, nominal = 2, variability = [-1  1], 1 occurrence 
size(M) 
ans = 
     1     3     4 
arraysize(M) 
ans = 
     4     1 

Check that result is valid. Use referencing to access parts of the [4-by-1] umat array and compare to the expected values. The first 4 examples should all be arrays full of 0 (zeros). The last two should be the value 5, and the uncertain real parameter a, respectively.

simplify(M(:,:,1) - [a b 1]) 
ans = 
     0     0     0 
simplify(M(:,:,2) - [-a -b 4+a]) 
ans = 
     0     0     0 
simplify(M(:,:,3) - [4 5 6]) 
ans = 
     0     0     0 
simplify(M(:,:,4) - [a 0 0]) 
ans = 
     0     0     0 
simplify(M(1,2,3))  % should be 5 
ans = 
     5 
simplify(M(1,3,2)-4)
Uncertain Real Parameter: Name a, NominalValue 4, variability = [-1  1] 

You can create a random 1-by-3-by-4 double matrix and stack this with M along the second array dimension, creating a 1-by-3-by-4-by-2 umat.

N = randn(1,3,4); 
M2 = stack(2,M,N); 
size(M2) 
ans = 
     1     3     4     2 
arraysize(M2) 
ans = 
     4     2 

As expected, both M and N can be recovered from M2.

d1 = simplify(M2(:,:,:,1)-M); 
d2 = simplify(M2(:,:,:,2)-N); 
[max(abs(d1(:))) max(abs(d2(:)))] 
ans = 
     0     0 

It is also possible to stack M and N along the 1st array dimension, creating a 1-by-3-by-8-by-1 umat.

M3 = stack(1,M,N); 
size(M3) 
ans = 
     1     3     8 
arraysize(M3) 
ans = 
     8     1 

As expected, both M and N can be recovered from M3.

d3 = simplify(M3(:,:,1:4)-M); 
d4 = simplify(M3(:,:,5:8)-N); 
[max(abs(d3(:))) max(abs(d4(:)))] 
ans = 
     0     0