Split matrix across the median
Mostra commenti meno recenti
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 Commenti
Azzi Abdelmalek
il 17 Dic 2012
Can you post an example and tell what are the expecting results
M
il 17 Dic 2012
M
il 17 Dic 2012
Walter Roberson
il 17 Dic 2012
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
il 17 Dic 2012
Walter Roberson
il 17 Dic 2012
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
il 17 Dic 2012
Risposta accettata
Più risposte (1)
Mark Whirdy
il 17 Dic 2012
Modificato: Mark Whirdy
il 17 Dic 2012
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 Commenti
Walter Roberson
il 17 Dic 2012
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
il 17 Dic 2012
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
il 17 Dic 2012
M
il 17 Dic 2012
Walter Roberson
il 17 Dic 2012
b = repmat(median(a,2),1,size(a,2));
is the correction for Mark's code.
M
il 17 Dic 2012
Walter Roberson
il 17 Dic 2012
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.
Categorie
Scopri di più su Matrices and Arrays in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!