Borrar filtros
Borrar filtros

How can I sort a matrix according to column IDs and identify same combinations?

1 visualización (últimos 30 días)
Hi,
I edit the question: I have matrix M with six columns and n rows:
M = [HomeID, WorkID, SchoolID, ShopID, timeS, timeP];
for example (lets say it is already sorted):
M = [101, 201, 301, 401, 5.46, 7.83;
101, 201, 301, 401, 3.63, 4.52;
101, 201, 301, 401, 2.785, 2.99;
102, 205, 301, 401, 3.53, 8.5;
102, 205, 301, 402, 3.48, 8.9];
I want to make it in the form of:
M2 = [101, 201, 301, 401, 3.958, 5.113;
102, 205, 301, 401, 3.53, 8.5;
102, 205, 301, 402, 3.48, 8.9];
I want to make calculation for each unique pair of SchoolID-WorkID combination within each unique pair of HomeID-WorkID combination and produce a matrix which will have no multiple Home-Work-School-Shop registers but the unique ones with the average values instead [M2(1,4)=(M(1,4)+M(2,4)+M(3,4))/3].
Thanks,
Iro
  6 comentarios
Zhang lu
Zhang lu el 3 de Mayo de 2013
102, 201, 301, 401, 3.53, 8.5; ? what is it from?
Iro
Iro el 3 de Mayo de 2013
Hi Zhang lu..Does it really matter? It is just random numbers I used to illustrate my question. :)

Iniciar sesión para comentar.

Respuesta aceptada

Jonathan Sullivan
Jonathan Sullivan el 3 de Mayo de 2013
Editada: Jonathan Sullivan el 3 de Mayo de 2013
Accumarray would come in handy here. It's a rather useful function, but might take a little bit of reading to understand it completely. Basically it takes in a list of coordinates (subscripts) and the values associated with them. It then does an operation on all those values that have the same subscripts, and outputs the resulting value as an entry in a matrix. In this case, you want to take the mean of those values that have the same HomeID WorkID SchoolID and ShopID.
Here's how we do it:
% Get the unique combinations, and the indexes
[HomeSchoolWorkShop_ID,~,y_ind] = unique(M(:,1:4),'rows');
x_ind = 1:size(M,2)-4;
% Make a list of the indexes and values
[X_Ind,Y_Ind] = meshgrid(x_ind,y_ind);
Inds = [Y_Ind(:) X_Ind(:)];
vals = reshape(M(:,5:end),[],1);
% Take the average
M2 = [HomeSchoolWorkShop_ID accumarray(Inds,vals,[],@mean)]
  1 comentario
Iro
Iro el 3 de Mayo de 2013
Editada: Iro el 3 de Mayo de 2013
Hi Jonathan, thanks for your answer. I did it and works! *Accumarray *looks good for my case if it can accommodate also more complex calculations, I will take a careful look. Cheers! /Iro

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by