I am looking for a faster way to compute a matrix using MATLAB:
Given an m-by-n matrix A, I would like to return a matrix B with the additions of all ith and the jth rows such that j >= i. E.g.,
Let A=[1 2 3 4; 2 3 4 5; 3 4 5 6], then B can be computed as
idx=1;
nbrows=size(A,1);
B=zeros(nbrows*(nbrows+1)/2,size(A,2)); % the size of B can be determined
for i = 1:nbrows
for j = i:nbrows
B(idx,:) = A(i,:) + A(j,:);
idx = idx + 1;
end
end
Now, I have a very large A, and I would like to know how to compute the matrix B in a more efficient way.
How can this computation be sped-up?
A=rand(5e3, 20), then the computation ofBmay takes a while. In fact, this kind of computations have to be repeated in my code, so that I need this step to be as fast as possible. - kaienfr