Is there an efficient way to incrementally update columns of a sparse Armadillo matrix? - sparse-matrix

I want to gradually add sparse columns to a sparse matrix. Something like:
for ( int ieig_insert= 0; ieig_insert < 5000 ; ieig_insert++ ) {
arma::sp_vec tempVec( nSources_Total ) ; // nSources_Total ~= 60,000
createColValues( ieig_insert, tempVec ); // ~95% sparse
sp_mat.col( ieig_insert )= tempVec ;
}
Yes, I can create a temporary vector<arma::sp_vec> to collect all the columns and then use a giant batch insertion constructor at the end, but that is inelegant. I tried the simpler approach above but it is maddeningly slow. It seems that this column insert should be efficient for CSC representation?

Related

Creating diagonal matrix from array of matrices in MATLAB

I am interested how to create a diagonal matrix from an array of matrices.
I created an array of matrices in MATLAB:
X<62x62x1000> it consists of 1000 matrices with dimensions 62x62
I want to create a matrix of dimensions 62000x62000 with 1000 sub matrices from array X along main diagonal.
Do you have any clue how to do this, except M=blkdiag(X(:,:,1), X(:,:,2), X(:,:,3)...) because that would be to much writing.
A possible solution
M = kron(speye(1000),ones(62));
M(logical(M)) = X(:);
With kron a 62000*62000 sparse matrix M is created that contains 1000 blocks of ones on its diagonal, then replace ones with elements of X.
You can flatten out your input matrix into a column vector using (:) indexing and then pass it to diag to place these elements along the diagonal of a new matrix.
result = diag(X(:))
This will order the elements along the diagonal in column-major order (the default for MATLAB). If you want a different ordering, you can use permute to re-order the dimensions prior to flattening.
It's important to note that your resulting matrix is going to be quite large. You could use spdiags instead to create a sparse diagonal matrix
spdiags(X(:), 0, numel(X), numel(X))
A very controversial eval call can solve this very lazily, although I suspect there is a much better way to do this:
evalstring = ['M=blkdiag('];
for i = 1:999
evalstring = [evalstring, 'X(:,:,', num2str(i),'),'];
end
evalstring = [evalstring, 'X(:,:,1000));'];
eval(evalstring);

How to repeat calculation for every value in an array and store the vector of each result in a new array in MATLAB [duplicate]

I've just started using for loops in matlab in programming class and the basic stuff is doing me fine, However I've been asked to "Use loops to create a 3 x 5 matrix in which the value of each element is its row number to the power of its column number divided by the sum of its row number and column number for example the value of element (2,3) is (2^3 / 2+3) = 1.6
So what sort of looping do I need to use to enable me to start new lines to form a matrix?
Since you need to know the row and column numbers (and only because you have to use loops), for-loops are a natural choice. This is because a for-loop will automatically keep track of your row and column number for you if you set it up right. More specifically, you want a nested for loop, i.e. one for loop within another. The outer loop might loop through the rows and the inner loop through the columns for example.
As for starting new lines in a matrix, this is extremely bad practice to do in a loop. You should rather pre-allocate your matrix. This will have a major performance impact on your code. Pre-allocation is most commonly done using the zeros function.
e.g.
num_rows = 3;
num_cols = 5;
M = zeros(num_rows,num_cols); %// Preallocation of memory so you don't grow your matrix in your loop
for row = 1:num_rows
for col = 1:num_cols
M(row,col) = (row^col)/(row+col);
end
end
But the most efficient way to do it is probably not to use loops at all but do it in one shot using ndgrid:
[R, C] = ndgrid(1:num_rows, 1:num_cols);
M = (R.^C)./(R+C);
The command bsxfun is very helpful for such problems. It will do all the looping and preallocation for you.
eg:
bsxfun(#(x,y) x.^y./(x+y), (1:3)', 1:5)

Subsetting a dataframe into an array

Is it possible to subset a data frame into an array?
I have a huge data frame and need to create subsets to work with them.
There is a long method where i create many variables but what about an array storing these subsets?
I want to subset based on the year(from 2004 to 2015) and store it into an array.
Here is my code.
for(i in (4:15))
{
v=2004
temp<-subset(LI,format(strptime(LI[,1],"%Y-%m-%d"),"%Y")==v)
Annual_LI[i]<-temp
v=v+1
}
The error which appears is
M_LI [i] < - temp :
Number of replacing elements is not a multiple of the length of replacement
How do i go about this?

Split matrix into several depending on value in Matlab

I have a cell array that I need to split into several matrices so that I can take the sum of subsets of the data. This is a sample of what I have:
A = {'M00.300', '1644.07';...
'M00.300', '9745.42'; ...
'M00.300', '2232.88'; ...
'M00.600', '13180.82'; ...
'M00.600', '2755.19'; ...
'M00.600', '15800.38'; ...
'M00.900', '18088.11'; ...
'M00.900', '1666.61'};
I want the sum of the second columns for each of 'M00.300', 'M00.600', and 'M00.900'. For example, to correspond to 'M00.300' I would want 1644.07 + 9745.42 + 2232.88.
I don't want to just hard code it because each data set is different, so I need the code to work for different size cell arrays.
I'm not sure of the best way to do this, I was going to begin by looping through A and comparing the strings in the first column and creating matrices within that loop, but that sounded messy and not efficient.
Is there a simpler way to do this?
Classic use of accumarray. You would use the first column as an index and the second column as the values associated with each index. accumarray works where you group values that belong to the same index together and you apply a function to those values. In your case, you'd use the default behaviour and sum things together.
However, you'll need to convert the first column into numeric labels. The third output of unique will help you do this. You'll also need to convert the second column into a numeric array and so str2double is a perfect way to do this.
Without further ado:
[val,~,id] = unique(A(:,1)); %// Get unique values and indices
out = accumarray(id, str2double(A(:,2))); %// Aggregate the groups and sum
format long g; %// For better display of precision
T = table(val, out) %// Display on a nice table
I get this:
>> T = table(val, out)
T =
val out
_________ ________
'M00.300' 13622.37
'M00.600' 31736.39
'M00.900' 19754.72
The above uses the table class that is available from R2013b and onwards. If you don't have this, you can perhaps use a for loop and print out each cell and value separately:
for idx = 1 : numel(out)
fprintf('%s: %f\n', val{idx}, out(idx));
end
We get:
M00.300: 13622.370000
M00.600: 31736.390000
M00.900: 19754.720000

matlab vectorization varying array

Can anyone tell me what the basic idea behind varying rows or columns in a matrix with respect to the row/column number is in matlab? I've been trying to replace all the columns in a given matrix by
i=1:101;
V=ones(121,101);
V_t=1000*10.^((i-1)/20);
e=V_arr(1:121)';
V_arr=V; V_arr(:,i)=V_t*e;
I know that the error lies in trying to replace a number of columns with respect to all rows, and I've seen an alternative, simpler method using repmat, but I'd like to know if there's a method similar to the one above.
Thanks.
One thing you can do is to use matrix multiplication, i.e. a n-by-1 array multiplied by an 1-by-m array creates a n-by-m array.
For example
ii = 1:101; %# 1-by-101
V_t = 1000*10.^((i-1)/20);
ee = ones(121,1); %# 121-by-1
V_arr = ee * V_t;

Resources