Group Identical Values in Matrix While Maintaining Original Indicies
Mostrar comentarios más antiguos
Hi I have a matrix that is 43885x1. The matrix is composed of dates from 1/20/2009 to 2/20/2009. The matrix flows in numerically increasing order but with duplicates for each date. I want to group each day into its own variable with corresponding indicies. So for a matrix for 1/20/2009 would be all the indicies that originally were 1/20/2009 from the original large matrix, i.e.
X1=[ 1 2 3 ... 8567]
And for 2/20/2009 it would read something like
X32=[ 37568 37569 37570 .... 43885]
5 comentarios
Iain
el 23 de Mayo de 2013
I think "unique" might have the outputs you want, though, it might not be in the form you need.
the cyclist
el 23 de Mayo de 2013
Iain, I suggest you put your answer as an answer, and not as a comment.
the cyclist
el 23 de Mayo de 2013
Nathan, how is your original matrix stored? Are the dates strings in a cell array, or datenums in a numerical array?
Iain
el 23 de Mayo de 2013
I did that earlier with a not-fully-explicit answer and got told off for making it an answer...
the cyclist
el 23 de Mayo de 2013
Editada: the cyclist
el 23 de Mayo de 2013
Hm. OK, I can see that point of view, too. Sorry you got "told off", though!
Respuesta aceptada
Más respuestas (2)
Sean de Wolski
el 23 de Mayo de 2013
Hi Nathan,
Here is the general workflow you will want to use:
- Use datenum() to convert the dates to serial date numbers
- Then, use unique() on this while capturing the first and thirs output:
[uv,~,idx] = unique(X);
- uv will contain the unique values and idx will be the size of x with each of its values pointing to the corresponding unique value.
From here it depends on what you want to do with this information. You have the indices, idx, where each index represents the value. You can use these as the subs in accumarray to perform a function on some other data. A better detailed example with all inputs, and expected outputs would help us help you with this.
1 comentario
David Sanchez
el 23 de Mayo de 2013
A=[ 1;1;2;2;2;3;3;3;4;4;4]; % example matrix
[B ind]=unique(A);
L = length(ind);
C = cell(L,1);
C{1} = A(1:ind(1));
for k = 2:L
C{k} = A( (ind(k-1)+1):ind(k) );
end
You will end up with a cell array whose cells contain a matrix for each date
Categorías
Más información sobre Resizing and Reshaping Matrices en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!