Initializing multi dimensional arrays in lua/torch - arrays

I have gone through a few tutorials about "arrays" in lua/torch and all I see is the word tensors. What exactly are they? How can i initialize a 2d "tensor"? I tried torch.Tensor{1,2,3} which gave me
1
2
3
and torch.Tensor{1,2,3;4,5,6} like in octave/MATLAB but this gave me another column vector
1
2
3
4
5
6
How can I get a 2d "tensor" like the following?
1 2 3
4 5 6
Also, I'm using torch to execute my lua files. Is there no other notation for arrays other than "tensors" i.e. is there no other way we can use to represent a matrix in torch?

torch.Tensor{{1, 2, 3}, {4, 5, 6}}
Have a look at this (or that) for more details on tensors.

Related

Match length of two unequal arrays in MATLAB?

Suppose I have 3 arrays of different length:
A[1 2 3 4 5 6]; B[1 2 4], C[0 1 5 6]
the MAXIMUM length the three arrays is 6.
So I want B and C to have 6 elements too. Furthermore, the last "filled" elements of new array should be the last element of the original array
At the end, new B should look like [1 2 4 4 4 4], C should look like [0 1 5 6 6 6], how do I implement this?
Thanks a lot!
You can do this using MATLAB's indexing operators. If you assign to elements past the end of an array, MATLAB will grow the array. For vectors (as in this case), you only need to specify a single subscript because the direction to extend the array is not ambiguous.
I'd do it like this - this doesn't assume that you know up-front which of the arrays is the largest. I'm using numel to compute the number of elements in each vector. I'm also relying on the fact that for one of the arrays, end+1:maxLen is an empty range, so no modifications are made in that case.
maxLen = max([numel(A), numel(B), numel(C)]);
A(end+1:maxLen) = A(end)
B(end+1:maxLen) = B(end)
C(end+1:maxLen) = C(end)

Using one array values to access another array elements Matlab

I am doing some 3D and 4D matrix manipulation in Matlab.
I have created a 2D array which row values contain the index values of interest in a 3D matrix.
Assuming array A of size (Nx2)
A=[2 3 1;5 6 2;7 9 3;3 3 4;1 5 5]
2 3 1
5 6 2
7 9 3
3 3 4
1 5 5
Then, I want to use these elements to manipulate matrix B of size (NxMxL)
B=rand(9,9,5);
So I want to set B(2,3,1)=0 which corresponds to A(1,:).
If I naively go B(A(1,:))=0 this doesn't return me the desired output.
What I understand is that Matlab translate this into B=B(:) which reshape the matrix into a 1xNML
and then returns me the elements 2, 3 and 1 of the reshaped matrix.
How can I avoid this and make it understand my argument B(A(1,:))=B(2,3,1)?
use sub2ind , for example zeroing all the elements in B using of the rows in A as indices:
B(sub2ind(size(B),A(:,1),A(:,2),A(:,3)))=0;

Excel 5 Dimensional arrays

How do I make a 5D array in excel? I have to later shift it to Matlab as a 5D single array. I have large amounts of EEG data that I need to sort into arrays.
Are you asking for help on VBA, or looking for help on how to encode a 5D array in an Excel sheet? This answer assumes the latter. There are many ways to do this, but here are two options (not necessarily good options, but hopefully they will help you think creatively about the problem).
(1) If the array is sparse, you can store the data in an excel sheet as a list of tuples of the form (dim1, dim2, dim3, dim4, dim5, value), preceded by the dimensionality of the array. For example, your Excel sheet could contain the following data:
5 9 9 7 8
1 2 9 6 4 10.1
4 9 3 7 2 18.9
3 2 1 7 8 19.2
and the corresponding MATLAB array would be defined as follows (assuming you want empty values to be NaNs):
mat = NaN(5, 9, 9, 7, 8);
mat(1, 2, 9, 6, 4) = 10.1;
mat(4, 9, 3, 7, 2) = 18.9;
mat(3, 2, 1, 7, 8) = 19.2;
(2) You can store the array as a simple list of values, again preceded by the array's dimensionality. The list could be indexed canonically based on how MATLAB vectorizes arrays. For example, your Excel sheet could contain the following data:
5 9 9 7 8
11.3
14.2
25.5
18.9
<etc.>
and the corresponding MATLAB array would be defined as follows (again assuming you want empty values to be NaNs):
mat = NaN(5, 9, 9, 7, 8);
mat(1) = 11.3;
mat(2) = 14.2;
mat(3) = 25.5;
mat(4) = 18.9;
<etc.>
Of course, in both cases, you can automate the loading of the matrices from Excel into MATLAB, using MATLAB functions such as xlsread and reshape.
And also there is nothing special about Excel files for the way I'm interpreting your question; if you want to go this route then CSV files might be a better option for storing the data.

Matlab, finding common values in two array

I want to find common values in two arrays(For example if A=[1 2 3 4 5 6] and B=[9 8 7 6 3 1 2] the result is ans=[1 2 3 6]).Is there any method without using loop?
Thanks
use intersect(A,B) to get the answer.
Another option is to use ismember, for example A(ismember(A,B)).

Matlab: Creating maps of groups and indicies of duplicates in an array

I have two Matlab arrays A(containing groups of numbers) and B(containing values that belong to the groups in A), there are repeats in array A
A = [1 1 1 2 2 3 4 4 4 4 4]
B = [1 2 3 3 5 4 4 1 6 7 8]
Now i would like to get the following two maps:
C = ['1': {1,2,3}, '2': {3,5}, '3':{4}, '4':{1,6,7,8}]
where C gives a map with the group number as index and related values in that particular group.
D = ['1':{2,4},'2':{1},'3':{4},'4':{1}]
Where D gives a map with the group number from A as index. The values are the group numbers from A for which there are repeated values in B for that particular sub group.
What is the most efficient way of dealing with this problem? Are maps a good data structure to store this kind of data. I know the first one can be dealt with a for loop which i would like to avoid.
I don't understand how you get to D.
For C, you can use accumarray:
C = accumarray(A,B,[],#(x){x})
C{1} is [1 2 3]

Resources