Search This Blog

Monday, September 03, 2007

initializing multi dimensional arrays in octave

In octave, multi dimensional arrays can be initialized using the reshape function. As an example, consider the following script.

$cat multidimensional_array.m
1;
a = zeros(1, 24);
for i=1:24
a(i) = i;
end
a
b = reshape(a, [4 3 2])

The output looks as follows

$octave -q
octave:1> multidimensional_array
a =

Columns 1 through 16:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Columns 17 through 24:

17 18 19 20 21 22 23 24

b =

ans(:,:,1) =

1 5 9
2 6 10
3 7 11
4 8 12

ans(:,:,2) =

13 17 21
14 18 22
15 19 23
16 20 24



octave:2>

No comments:

Followers