Split matrix across the median

How do I split a matrix across its median? Suppose I have a 5x10 matrix sorted in ascending order. I want to split this into two 5x5 matrices by finding the median of each row.
Then each sub-matrix will be further split k-times, for a huge matrix. The number of rows will remain constant.

댓글 수: 7

Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 17일
Can you post an example and tell what are the expecting results
M
M 2012년 12월 17일
Suppose
a =
0 2 4 6 8 10 12 14
1 2 3 4 5 6 7 8
10 9 8 7 6 5 4 3
3 4 5 6 7 8 9 10
7 6 5 4 3 2 1 0
I need to split this into
a1 =
0 2 4 6
1 2 3 4
10 9 8 7
3 4 5 6
7 6 5 4
and
a2 =
8 10 12 14
5 6 7 8
6 5 4 3
7 8 9 10
3 2 1 0
M
M 2012년 12월 17일
How about splitting across the mean?
Walter Roberson
Walter Roberson 2012년 12월 17일
You said that the matrix was sorted in ascending order, but your "a" matrix here is in descending order for rows 3 and 5.
M
M 2012년 12월 17일
Yes, but I can sort it if needed by using A = sort(a,2);
Walter Roberson
Walter Roberson 2012년 12월 17일
We need a specific statement from you about whether there can be any duplicates, and if so then how do you want to handle the case where the median happens to be duplicated.
The answer I gave is for the situation where duplicates are allowed and it is acceptable to split duplicate medians across the two arrays.
M
M 2012년 12월 17일
Yes, there can be duplicates

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

 채택된 답변

Walter Roberson
Walter Roberson 2012년 12월 17일

0 개 추천

If the rows are already sorted in ascending order, then the median is, by definition, right at the middle, so just index appropriately.
A1 = A(:,1:end/2);
A2 = A(:,end/2+1:end);

댓글 수: 4

Mark Whirdy
Mark Whirdy 2012년 12월 17일
this is true but if there are repeats of the median number then some will be spplit into the top matrix snd some into the bottom, i don't know if this is a desired behaviour ... or if repeats are possible
Walter Roberson
Walter Roberson 2012년 12월 17일
Note: this code is also fine with cases where the rows are sorted in descending order, or where some rows are ascending and other rows are descending. It does not, however, work for cases where the rows are not individually sorted.
For the situation where the array might have an odd number of columns, and the middle value should just be left out, modify to
A1 = A(:,1:floor(end/2));
A2 = A(:,ceil(end/2)+1:end);
M
M 2012년 12월 17일
편집: M 2012년 12월 17일
Thank you, this works on the example matrix. Now I have to test it on the actual data matrix which is 12x416 in size

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

추가 답변 (1개)

Mark Whirdy
Mark Whirdy 2012년 12월 17일
편집: Mark Whirdy 2012년 12월 17일

0 개 추천

a = rand(5,10);
b = repmat(median(a,1),10);
c1 = a; c1(a<b) = NaN;
c2 = a; c2(a>=b) = NaN;
.... what you're looking for? like Azzi, itshard to know without an example

댓글 수: 7

Walter Roberson
Walter Roberson 2012년 12월 17일
This doesn't create two 5 x 5 matrices, and it also doesn't do a good split if the median is repeated. e.g., a = 2 * ones(5,10) would end up with c1 untouched and with c2 all NaN with that code.
Mark Whirdy
Mark Whirdy 2012년 12월 17일
2 5x5 matrices aren't a sensible solution if repeats are possible,.on 2nd point it depends on required behaviour as I'd rather group equivalent numbers together than split arbitrarily... depends on desired behaviou
M
M 2012년 12월 17일
The above code creates gives the error:
Error using < Matrix dimensions must agree.
M
M 2012년 12월 17일
I have posted an example above. This would be further split into four 5x2 matrices.
Walter Roberson
Walter Roberson 2012년 12월 17일
b = repmat(median(a,2),1,size(a,2));
is the correction for Mark's code.
M
M 2012년 12월 17일
Can you please explain the above line? Why is it needed?
I tried it but it creates another 5x10 matrix
Walter Roberson
Walter Roberson 2012년 12월 17일
Mark's code works by creating a "b" matrix the same size as the "a" matrix, with "b" containing copies of the row medians, and then doing an element-by-element comparison of the matrices. The result after his suggested computations is two matrices the same size as the original matrix, but with NaN in places indicating elements that were split over into the other matrix.

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

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

태그

질문:

M
M
2012년 12월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by