MATLAB Merging Arrays -
i unable figure out how merge 2 arrays. data arrays , b.
a = [ 0 0; 0 0; 2 2; 2 2;] b = [ 1 1; 1 1; 3 3; 3 3; 4 4; 4 4; 5 5; 5 5;]
and need final array "c" after merging:
c = [ 0 0; 0 0; 1 1; 1 1; 2 2; 2 2; 3 3; 3 3; 4 4; 4 4; 5 5; 5 5;]
i've tried using different ways reshaping each array , trying use double loop haven't got work yet.
in actual data inserting 9 rows of array b following 3 rows of array , repeated 100 times. so, there 12 new merged rows (3 rows array , 9 rows array b) repeated 100 times final row number == 1200. array actual data has 300 rows , actual array b data has 900 rows
thanks,
here's solution using reshape
:
a = [ 6 6; 3 3; 5 5; 4 4;] b = [ 0 0; 21 21; 17 17; 33 33; 29 29; 82 82;] a_count = 2; b_count = 3; w = size(a,2); %// width = number of columns ar = reshape(a,a_count,w,[]); br = reshape(b,b_count,w,[]); cr = [ar;br]; c = reshape(cr,[],w)
the []
in reshape means "how ever many need total number of elements". if have 12 elements in b
, do:
br = reshape(b,3,2,[]);
we're reshaping b
3x2xp
3-dimensional matrix. since total number of elements 12, p = 2
because 12 = 3x2x2
.
output:
a = 6 6 3 3 5 5 4 4 b = 0 0 21 21 17 17 33 33 29 29 82 82 c = 6 6 3 3 0 0 21 21 17 17 5 5 4 4 33 33 29 29 82 82
Comments
Post a Comment