To extract specific columns from large matrices, is it faster to use a vector of column indices together with the [ ] brackets or to use the submat(...) command?
1 Answer
0
You probably won't notice any direct speed difference with manually specifying the indices as opposed to using subvec. However, your options are narrowed depending on your use-case, as well as the elegance of the code involved.
Using a basic representation of x:
>> x = reshape(seqa(1,1,25), 5, 5);
>> x
1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
6.0000000 7.0000000 8.0000000 9.0000000 10.000000
11.000000 12.000000 13.000000 14.000000 15.000000
16.000000 17.000000 18.000000 19.000000 20.000000
21.000000 22.000000 23.000000 24.000000 25.000000
1) If you are extracting the same column for every row:
>> x[.,2]
2.0000000
7.0000000
12.000000
17.000000
22.000000
is the most straight-forward
If you plan on extracting multiple columns in this manner, you could gain a significant speed advantage by transposing the matrix first.
2) Extracting all rows with a specific column for each row.
Note that this method requires the column vector to equal the rows of the input matrix.
>> subvec(x, seqa(1,1,rows(x)));
1.0000000
7.0000000
13.000000
19.000000
25.000000
3) Extracting specific columns (not all) for specific row(s):
>> c = {1,3,5};
>> x[.,c]
1.0000000 3.0000000 5.0000000
6.0000000 8.0000000 10.000000
11.000000 13.000000 15.000000
16.000000 18.000000 20.000000
21.000000 23.000000 25.000000
Your Answer
1 Answer
You probably won't notice any direct speed difference with manually specifying the indices as opposed to using subvec. However, your options are narrowed depending on your use-case, as well as the elegance of the code involved.
Using a basic representation of x:
>> x = reshape(seqa(1,1,25), 5, 5);
>> x
1.0000000 2.0000000 3.0000000 4.0000000 5.0000000
6.0000000 7.0000000 8.0000000 9.0000000 10.000000
11.000000 12.000000 13.000000 14.000000 15.000000
16.000000 17.000000 18.000000 19.000000 20.000000
21.000000 22.000000 23.000000 24.000000 25.000000
1) If you are extracting the same column for every row:
>> x[.,2]
2.0000000
7.0000000
12.000000
17.000000
22.000000
is the most straight-forward
If you plan on extracting multiple columns in this manner, you could gain a significant speed advantage by transposing the matrix first.
2) Extracting all rows with a specific column for each row.
Note that this method requires the column vector to equal the rows of the input matrix.
>> subvec(x, seqa(1,1,rows(x)));
1.0000000
7.0000000
13.000000
19.000000
25.000000
3) Extracting specific columns (not all) for specific row(s):
>> c = {1,3,5};
>> x[.,c]
1.0000000 3.0000000 5.0000000
6.0000000 8.0000000 10.000000
11.000000 13.000000 15.000000
16.000000 18.000000 20.000000
21.000000 23.000000 25.000000