필터 지우기
필터 지우기

partitioning a matrix based on another

조회 수: 2 (최근 30일)
Rabeya
Rabeya 2012년 5월 17일
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
Thomas
Thomas 2012년 5월 17일
do you always have two elements in ng? or can you have more?
Jan
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 5월 17일
k = mat2cell(X,ng,size(X,2))
[x1,x2] = k{:}

추가 답변 (1개)

Honglei Chen
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
Rabeya
Rabeya 2012년 5월 17일
I want to partition it, depending on ng
Honglei Chen
Honglei Chen 2012년 5월 18일
Did you try? Each cell of y holds a partition.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by