I use these two families of programming languages quite often. I did C/C++ first then Octave/Matlab later on. The two differ in how they handle multi-directional array, especially ones more than 3-nest deep.
We know that computer memory uses (virtual?) contiguous address of memory--so by any means they are 1-dimensional. But program (and in this regard, the programming language) needs to map them somehow to fit the continuous address arrangement abstraction.
On 3-dimensional array, with C it is:unsigned int val[2][4][3];
for (i=0; i<2; i++){
for (j=0; j<4; j++){
for (k=0; k<3; k++){
printf("%d %d %d %p\n", i, j, k, &(val[i][j][k]));
}
}
}
*
0 0 0 00007ff73f2dd040
0 0 1 00007ff73f2dd044
0 0 2 00007ff73f2dd048
0 1 0 00007ff73f2dd04c
0 1 1 00007ff73f2dd050
0 1 2 00007ff73f2dd054
0 2 0 00007ff73f2dd058
0 2 1 00007ff73f2dd05c
0 2 2 00007ff73f2dd060
0 3 0 00007ff73f2dd064
0 3 1 00007ff73f2dd068
0 3 2 00007ff73f2dd06c
1 0 0 00007ff73f2dd070
1 0 1 00007ff73f2dd074
1 0 2 00007ff73f2dd078
1 1 0 00007ff73f2dd07c
1 1 1 00007ff73f2dd080
1 1 2 00007ff73f2dd084
1 2 0 00007ff73f2dd088
1 2 1 00007ff73f2dd08c
1 2 2 00007ff73f2dd090
1 3 0 00007ff73f2dd094
1 3 1 00007ff73f2dd098
1 3 2 00007ff73f2dd09c
*/
See the memory address changes LSB (least significant [4] bytes, lol) based on the variable's LSB (last-shown beacon, I am making up this abbreviation).
With Octave, the story is slightly different. The two pages from the official documentation:
- https://docs.octave.org/v4.0.3/Index-Expressions.html
- https://docs.octave.org/v5.1.0/Advanced-Indexing.html
don't help much as they only give example up to only 2-dimension.
>> val= reshape(1:2*4*3, 2,4,3);
>> val =
ans(:,:,1) =
1 3 5 7
2 4 6 8
ans(:,:,2) =
9 11 13 15
10 12 14 16
ans(:,:,3) =
17 19 21 23
18 20 22 24
>> val(:,1,:)
ans(:,:,1) =
1
2
ans(:,:,2) =
9
10
ans(:,:,3) =
17
18
>> val(1,:,:)
ans =
ans(:,:,1) =
1 3 5 7
ans(:,:,2) =
9 11 13 15
ans(:,:,3) =
17 19 21 23
See.... the first index in the element actually iterates first, i.e. the LSB now becomes MSB. It is somewhat nice to have first index as row vector/identifier and second index for the column. But what about third index? Fourth?
Well, sometimes you just have embrace the quirk.
0 rants:
Post a Comment