I would like to have a program that makes the following actions:
-
Read several matrices having the same size (1126×1440 double)
-
Select the most occuring value in each cell (same i,j of the matrices)
-
write this value in an output matrix having the same size 1126×1440 in the corresponding i,j position, so that this output matrix will have in each cell the most occurent value from the same position of all the input matrices.
Here is the code you need. I have introduced a number of constants:
I first generate example matrices with rand. Matrices are changed to vectors and concatenated in the CC matrix. Hence, the dimensions of CC are [m*n, nmatrices]. Every row of CC holds individual (i,j) values for all matrices – those you want to analyze.
Now we do the real work. I have to transpose CC, because matlab works on column-based matrices, so I want to analyze individual columns of CC, not rows. Next, using histc I find the most frequently occuring values in every column of CC, i.e. in (i,j) entries of all matrices. histc counts the values that fall into given bins (in your case – 1:maxval) in every column of CC.
counts have dimensions [maxval, m*n] – for every (i,j) of your original matrices you know the number of times a given value from 1:maxval is represented. The last thing to do now is to sort the counts and find out, which is the most frequently occuring one. I do not need the sorted counts, I need the permutation that will tell me, which entry from counts has the highest value. That is exactly what you want to find out.
B is what you want.