开发者

Repmat a matrix rows as per another matrix - MATLAB

I have 2 matrices with the same IDs. mat1 has multiple rows per ID while mat2 has ONLY 1 row per ID. Matrices are pre-sorted.

% COL1 -> ID
mat1 = [ 20 2008 0.11 ; 20 2010 0.22  ; 30 2001 0.99 ; 40 2011 0.11 ; 40 2011 0.22 ; 40 2012 0.11 ] ; 
mat2 = [ 20 0.88 ; 30 0.11 ; 40 0.99 ] ;

mat2 IDs need to be repeated as per their corresponding number of rows in mat1.

mat2 = [ 20 0.88 ; 20 0.88 ; 30 0.11 ; 40 0.99 ; 40 0.99 ; 40 0.99 ] ;

Can you suggest an answer? On m开发者_开发技巧y own, I thought of using accmarray etc to get row count per ID and then may be repmat mat2. Thanks.


Try this:

[b,m,n] = unique(mat1(:,1));
mat3 = mat2(n,:)

Hope this helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜