partitioning a matrix based on another
3 次查看(过去 30 天)
显示 更早的评论
I have two matrices,say, X=[1 2 2; 2 3 3; 3 5 5 ;6 1 2; 3 4 3] and ng=[2;3]. I want to partition X based on ng, i.e.
x1=[1 2 2; 2 3 3], i.e. taking the ng(1) number of rows x2=[3 5 5 ;6 1 2; 3 4 3] , ng(2) number of rows where
X=[x1;x2].
I can do it using a for loop, but is there any efficient way to do this? This is just an example, there can be a larger ng, and also x
2 个评论
Jan
2012-5-17
I assume, the FOR loop will be the most efficient method - it is required inside MAT2CELL also. If you post your code, we could potentially find improvements.
采纳的回答
更多回答(1 个)
Honglei Chen
2012-5-17
If your matrix can be exactly partitioned, you can use mat2cell. In your problem, X has 5 lines, which cannot be evenly divided by your ng, so I added one line
X=[1 2 2; 2 3 3; 3 5 5 ;6 1 2; 3 4 3; 5 3 2]
y = mat2cell(X,ng(1)*ones(1,size(X,1)/ng(1)),ng(2)*ones(1,size(X,2)/ng(2)))
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!