开发者

Repmat a matrix rows as per another matrix - MATLAB

开发者 https://www.devze.com 2023-04-05 15:26 出处:网络
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.

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

精彩评论

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

关注公众号