merge two matrices together

9 views (last 30 days)
Niki
Niki on 21 May 2013
I have two matrices as follows
-----------------------------------------
40 40 40 40
41 41 41 41
42 42 42 42
43 44 43 44
43 45 44 44
44 48 45 49
45 50 49 51
50 51 51 52
51 52 52 53
52 53 53 54
------------------------------------------
1282 1687 2501 1466
870 929 644 1141
345 103 736 133
765 7484 1434 3267
2796 461 7021 2143
4689 212 363 241
549 320 343 218
219 761 415 249
187 316 684 868
85 309 271 395
----------------------------------------
Each column and row of the first matrix corresponds to the second matrix
I would like to generate a matrix as follows
40 1282 1687 2501 1466
41 870 929 644 1141
42 345 103 736 133
43 2796+765 0 1434 0
44 4689 7484 7021 3267+2143
45 549 461 363 0
46 0 0 0 0
47 0 0 0 0
48 0 212 0 0
49 0 0 343 241
50 219 320 0 0
51 189 761 415 218
52 85 316 684 249
53 0 309 271 868
54 0 0 0 395
As it can be seen , the first matrix was merged and one column which present all the data of the first matrix is appeared. each row and column of the second matrix corresponds to the first matrix and those that had similar values are sum and those values that do not exist a zero will be replaced.

Accepted Answer

Roger Stafford
Roger Stafford on 22 May 2013
If M1 and M2 are your two arrays (of like size), get your desired result in array M3:
[m,n] = size(M1);
[u,~,p] = unique(M1(:));
q = reshape(repmat(1:n,m,1),[],1);
M3 = [u,accumarray([p,q],M2(:),[size(u,1),n])];

More Answers (3)

José-Luis
José-Luis on 21 May 2013
ii = [40 40 40 40
41 41 41 41
42 42 42 42
43 44 43 44
43 45 44 44
44 48 45 49
45 50 49 51
50 51 51 52
51 52 52 53
52 53 53 54];
d = [1282 1687 2501 1466
870 929 644 1141
345 103 736 133
765 7484 1434 3267
2796 461 7021 2143
4689 212 363 241
549 320 343 218
219 761 415 249
187 316 684 868
85 309 271 395];
left_side = (min(ii):max(ii))';
mySum = @(idx) sum(d.*(ii==idx),1);
your_result = [left_side cell2mat(arrayfun(@(x) mySum(x),left_side, 'uniformoutput', false))];
  2 Comments
Niki
Niki on 21 May 2013
That is much better but the last two rows are missing , can you please try to get the same results as I mentioned above?
José-Luis
José-Luis on 21 May 2013
Edited: José-Luis on 21 May 2013
left_side = (min(ii(:)):max(ii(:)))'; %To get the proper range.
mySum = @(idx) sum(d.*(ii==idx),1);
your_result = [left_side cell2mat(arrayfun(@(x) mySum(x),left_side,'uniformoutput',false))];
Please accept an answer if it helps you.

Sign in to comment.


Iain
Iain on 21 May 2013
You ought to be able to mod this to give you exactly what you want - I've made some assumptions that are probably not true.
for i = 1:Rows
New(i,1) = i;
for j = 1:Columns
New(i,1+j) = sum(Old(Indices(i,j),j));
end
end
  7 Comments
Iain
Iain on 21 May 2013
m1 = indices = [40 40 40 40....
m2 = Old = [1282 ...
Niki
Niki on 21 May 2013
unfortunately does not work , please perform it yourself you will see that does not work

Sign in to comment.


Andrei Bobrov
Andrei Bobrov on 21 May 2013
Edited: Andrei Bobrov on 21 May 2013
ii = [40 40 40 40
41 41 41 41
42 42 42 42
43 44 43 44
43 45 44 44
44 48 45 49
45 50 49 51
50 51 51 52
51 52 52 53
52 53 53 54];
d = [1282 1687 2501 1466
870 929 644 1141
345 103 736 133
765 7484 1434 3267
2796 461 7021 2143
4689 212 363 241
549 320 343 218
219 761 415 249
187 316 684 868
85 309 271 395];
a = min(ii(:)):max(ii(:));
[l1, l2] = ismember(ii,a);
i1 = [l2(l1) kron((1:size(ii,2))',ones(size(ii,1),1))];
out = [a(:), accumarray(i1,d(:))]
  2 Comments
Niki
Niki on 21 May 2013
Thanks for your effort but it does not work completely, if you have look , I don't get the first column values with this script nor do I get some rows

Sign in to comment.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!