Split matrix across the median

7 Ansichten (letzte 30 Tage)
M
M am 17 Dez. 2012
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 Kommentare
Walter Roberson
Walter Roberson am 17 Dez. 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
M am 17 Dez. 2012
Yes, there can be duplicates

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Dez. 2012
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 Kommentare
Walter Roberson
Walter Roberson am 17 Dez. 2012
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 am 17 Dez. 2012
Bearbeitet: M am 17 Dez. 2012
Thank you, this works on the example matrix. Now I have to test it on the actual data matrix which is 12x416 in size

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Mark Whirdy
Mark Whirdy am 17 Dez. 2012
Bearbeitet: Mark Whirdy am 17 Dez. 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 Kommentare
M
M am 17 Dez. 2012
Can you please explain the above line? Why is it needed?
I tried it but it creates another 5x10 matrix
Walter Roberson
Walter Roberson am 17 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by