## How to create multidimensional arrays of distribution objects?

### Alex (view profile)

on 2 Oct 2013
Latest activity Commented on by Alex

on 3 Oct 2013

### Tom Lane (view profile)

Hi I am just working on a larger program that works with two random distributed Variables:

X for incoming products(Production) Y for outgoing products(Demand)

both random variables have 2 parameters, so a and b belong to X and c and d belong to Y. I am now working on a combination of both and need a combined distribution stored in a 4 dimensional array to reflect all possible combinations. My initial guess to create an "empty" array of the matching dimensions was just:

```    a=[1 2 3 4];
b=[5 6];
c=[7 8 9];
d=[1 2 3 4 5];```
```    for i=1:length(a)
for j=1:length(b)
for k=1:length(c)
for l=1:length(d)
test(i,j,k,l)=makedist('normal',0,0);
end
end
end
end```

the problem is now that matlab seems to have no default distribution-object included in the libraries and following error occurs:

During array expansion: No default is defined for class 'prob.NormalDistribution'. Method 'getDefaultScalarElement' in superclass prob.ProbabilityDistribution is missing or incorrectly defined.

I was lucky find a solution for 2 dimensions that just sets the elements before a default element is needed and i expanded this to a 3 dimensional array. However, if i try to expand the solution in the same way to 4 dimensions the error occurs again:

```clc
clear
a=[100 5 5 7];
b=[10 4 4 5 6];
c=[100 2 6 7 9 9];
d=[1 4 5 5];
```
```for i=1:length(a)
test(i,:)=makedist('normal',0,0);
for j=1:length(b)
test (i,j)=makedist('normal',0,0);
end
end
```
```test
```
```for i=1:length(a)
test2(i,:,:)=makedist('normal',0,0);
for j=1:length(b)
test2 (i,j,:)=makedist('normal',0,0);
for k=1:length(c)
test2 (i,j,k)=makedist('normal',0,0);
end
end
end
```
```test2
```
```for i=1:length(a)
test2(i,:,:,:)=makedist('normal',0,0);
for j=1:length(b)
test2 (i,j,:,:)=makedist('normal',0,0);
for k=1:length(c)
test2 (i,j,k,:)=makedist('normal',0,0);
for l=1:length(d)
test2(i,j,k,l)=makedist('normal',0,0);
end
end
end
end
```
```test3
```

While the first two versions are running perfectly, even as intended and give the matching distribution-object arrays, but the 4 dimensional one gives following error:

```During array expansion:
No default is defined for class 'prob.NormalDistribution'.
Method 'getDefaultScalarElement' in superclass prob.ProbabilityDistribution is missing or incorrectly defined.
```
```Error in Untitled (line 29)
test3(i,:,:,:)=makedist('normal',0,0);
```

When i define the 3 dimensional array first, set test3=test2, the error shifts to the line

test3(i,j,k,l)=makedist('normal',0,0);

as soon as d=[ ] has more than one element. i also tried to set the d loop around the other 3 loops but the error always occurs when the pointer of the fourth dimension shifts to 2. I dont know why this error occurs only with 4 dimensions, to my understanding it should also occur with 3 dimensions, or both versions should run propperly, i cant see why one works and the other one not.

It would be very kind if somebody could help me with this problem or give me a hint how to create this AxBxCxD array of distribution objects in another way. Thank you for your help

Matt J

on 2 Oct 2013

Alex

### Alex (view profile)

on 2 Oct 2013

Sorry, I didnt see that button, i reformated it, thank you

## Products

### Tom Lane (view profile)

on 2 Oct 2013

This looks like something that we should fix in a future release. Meanwhile, perhaps you would find this work-around useful:

```a = makedist('normal');           % create a single distribution object
a = repmat(a,[2 3 4 5]);          % expand it to the desired size
a(2,3,4,5) = makedist('Weibull'); % fill in the values you really want
```

Alex

### Alex (view profile)

on 3 Oct 2013

Ah great, that is the perfect solution, thank you very much!

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi