How can I squeeze singleton dimension after getting a SubArray?

630 Views Asked by At

I have a two dimensional array 'v' representing a list of vectors And I have a three dimensional array 'a' that represents a list of matrices

ILArray v = counter(2, 3);
ILArray a = counter(2, 3, 3);

Now I want to take a subarray of both and do a matrix multiply:

ILArray av = multiply(a[1,full,full], v[1,full]);

It will complain that the dimensions do not match and this is because the dimensions of a[1,full,full] is (1,3,3) and of v[1,full] is (1,3). In other words, the subarrays contain a singleton dimension. In matlab I can remove these singleton dimensions with squeeze(). But how can I do that in ILNumerics?


I just discovered that if the singleton dimension are trailing, then they are automagically removed by ILNumerics, so this works:

ILArray v = counter(3, 2);
ILArray a = counter(3, 3, 2);
ILArray av = multiply(a[full,full, 1], v[full, 1]);

Also, a[full,full,1] has dimension (3,3) and not (3,3,1).

Does that mean that ILNumerics does not support non-trailing singleton dimensions? In other words, does not support strided arrays in calculations but only contiguous ones? That would be rather restrictive to say the least.

Thanks, Luc

1

There are 1 best solutions below

6
On

Shouldn’t a simple reshape do the trick?

ILArray<int> I = ILMath.counter<int>(1,1,ILMath.size(5,4,3));

I
<Int32> [5,4,3]
    [0]: (:,:,0)
    [1]:          1          6         11         16
    [2]:          2          7         12         17
    [3]:          3          8         13         18
    [4]:          4          9         14         19
    [5]:          5         10         15         20
    [6]: (:,:,1)
    [7]:         21         26         31         36
    [8]:         22         27         32         37
    [9]:         23         28         33         38
    [10]:         24         29         34         39
    [11]:         25         30         35         40
    [12]: (:,:,2)
    [13]:         41         46         51         56
    [14]:         42         47         52         57
    [15]:         43         48         53         58
    [16]:         44         49         54         59
    [17]:         45         50         55         60

I["0;:;:"]
<Int32> [1,4,3]
    [0]: (:,:,0)
    [1]:          1          6         11         16
    [2]: (:,:,1)
    [3]:         21         26         31         36
    [4]: (:,:,2)
    [5]:         41         46         51         56

ILMath.reshape(I["0;:;:"],4,3)
<Int32> [4,3]
    [0]:          1         21         41
    [1]:          6         26         46
    [2]:         11         31         51
    [3]:         16         36         56

Having a squeeze function would be more convenient. You may open a feature request on the bugtracker in order to have it implemented in one of the next releases: http://ilnumerics.net/mantis