Thread Subject: Finding Median of multiple matrices

Subject: Finding Median of multiple matrices

From: Subrat Swain

Date: 24 Nov, 2008 20:54:02

Message: 1 of 4

Hi all,
I want to find the median of a matrx in MATLAB and I need the appropriate code for it. For example my input matrix is given as
C = 0 2 6
      5 0 8 ---------------- Ist 3*3 matrix (A)
      7 4 0
      0 7 6
      5 0 1 --------------- 2nd 3*3 matrix (B)
      3 2 0

The resulting median should be such that the first element of the 'A' matrix ,
(C(1,1) - 0) should get associated with the first element of B matrix, (C(4,1) - 0) and the corresponding median should be found out between these 2 numbers. Similarly the median should be found out for the remaining elements of A and B matrices. My output median matrix should be

D = 0 4.5 6
      5 0 4.5
      5 3 0

I appreciate for the help.

Thanks and Regards,
Subrat Kumar Swain.

Subject: Finding Median of multiple matrices

From: alistair templeton

Date: 24 Nov, 2008 22:03:02

Message: 2 of 4

"Subrat Swain" <swain.subrat01@gmail.com> wrote in message <ggf499$b1l$1@fred.mathworks.com>...
> Hi all,
> I want to find the median of a matrx in MATLAB and I need the appropriate code for it. For example my input matrix is given as
> C = 0 2 6
> 5 0 8 ---------------- Ist 3*3 matrix (A)
> 7 4 0
> 0 7 6
> 5 0 1 --------------- 2nd 3*3 matrix (B)
> 3 2 0
>
> The resulting median should be such that the first element of the 'A' matrix ,
> (C(1,1) - 0) should get associated with the first element of B matrix, (C(4,1) - 0) and the corresponding median should be found out between these 2 numbers. Similarly the median should be found out for the remaining elements of A and B matrices. My output median matrix should be
>
> D = 0 4.5 6
> 5 0 4.5
> 5 3 0
>
> I appreciate for the help.
>
> Thanks and Regards,
> Subrat Kumar Swain.
>

You can either stack the matrices so that
Mymatrix(:,:,1) = A
Mymatrix(:,:,2) = B

and just take the median(Mymatrix,3)

or you can do the
median([A(:) B(:)],2)

and reshape it if you need to.

Subject: Finding Median of multiple matrices

From: Subrat Swain

Date: 24 Nov, 2008 23:21:02

Message: 3 of 4

Hi Alistair,
Thanks a lot for the help. But could you please do another favor. Is it possible to
find the median of corresponding elements of n number of matrices instead of only 3 matrices. As per your e-mail the syntax
Mymatrix(:,:,1) = A
Mymatrix(:,:,1) = B
 median([A(:) B(:) C(:)],2) gives me the median for the 3 matrices only. Is it possible to put a loop for 'n' number of matrices and find the corresponding median. If it is so
can you please provide me the code or syntax for it.

Appreciate for your help.
Regards,
Subrat.
  
"Alistair Templeton" <bigalt2000@yahoo.com> wrote in message <ggf8am$mmo$1@fred.mathworks.com>...
> "Subrat Swain" <swain.subrat01@gmail.com> wrote in message <ggf499$b1l$1@fred.mathworks.com>...
> > Hi all,
> > I want to find the median of a matrx in MATLAB and I need the appropriate code for it. For example my input matrix is given as
> > C = 0 2 6
> > 5 0 8 ---------------- Ist 3*3 matrix (A)
> > 7 4 0
> > 0 7 6
> > 5 0 1 --------------- 2nd 3*3 matrix (B)
> > 3 2 0
> >
> > The resulting median should be such that the first element of the 'A' matrix ,
> > (C(1,1) - 0) should get associated with the first element of B matrix, (C(4,1) - 0) and the corresponding median should be found out between these 2 numbers. Similarly the median should be found out for the remaining elements of A and B matrices. My output median matrix should be
> >
> > D = 0 4.5 6
> > 5 0 4.5
> > 5 3 0
> >
> > I appreciate for the help.
> >
> > Thanks and Regards,
> > Subrat Kumar Swain.
> >
>
> You can either stack the matrices so that
> Mymatrix(:,:,1) = A
> Mymatrix(:,:,2) = B
>
> and just take the median(Mymatrix,3)
>
> or you can do the
> median([A(:) B(:)],2)
>
> and reshape it if you need to.

Subject: Finding Median of multiple matrices

From: Jos

Date: 25 Nov, 2008 13:13:02

Message: 4 of 4

"Subrat Swain" <swain.subrat01@gmail.com> wrote in message <ggfcsu$foh$1@fred.mathworks.com>...
> Hi Alistair,
> Thanks a lot for the help. But could you please do another favor. Is it possible to
> find the median of corresponding elements of n number of matrices instead of only 3 matrices. As per your e-mail the syntax
> Mymatrix(:,:,1) = A
> Mymatrix(:,:,1) = B
> median([A(:) B(:) C(:)],2) gives me the median for the 3 matrices only. Is it possible to put a loop for 'n' number of matrices and find the corresponding median. If it is so
> can you please provide me the code or syntax for it.
>
...

use cat to stack all N-by-M matrices
 
MM = cat(3,A,B,C,D,E,...) ;
median(MM,3)
 
hth
Jos

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com