I have the following data.
- a row vector idx1 of length n1
- an m x n1 matrix A1
- a row vector idx2 of length n2
- an m x n2 matrix A2
The row vectors idx1 and idx2 label the columns of A1 and A2, respectively. Essentially, I want to merge the columns of A1 and A2 according to the labels in idx1 and idx2. I think it's easiest to give some code I already have that does the job.
idx = union(idx1,idx2);
A = zeros(size(A1,1),length(idx));
for i = 1:length(idx)
j1 = find(idx1 == idx(i),1);
if ~isempty(j1)
A(:,i) = A(:,i) + A1(:,j1);
end
j2 = find(idx2 == idx(i),2);
if ~isempty(j2)
A(:,i) = A(:,i) + A2(:,j2);
end
end
Now, my problem is that I want to carry out this operation efficiently, sometimes on sparse matrices. Is there a faster way than what I have? Does the answer change if A1 and A2 are sparse?
idx1
has no repeated values, and same foridx2
? Otherwise I think your code doesn't work – Luis Mendo