Effacer les filtres
Effacer les filtres

How to assing incrementing numbers to identical values in a column?

1 vue (au cours des 30 derniers jours)
A. Goeh
A. Goeh le 22 Août 2016
Modifié(e) : Andrei Bobrov le 22 Août 2016
Hello to all,
my ultimate goal of this question is to find a solution for the following problem: I am searching for the index of the first and last day of a month of one year specifically. To specify my question here a design of my data :
Column 1(Year) Column 2(Month) Column 3(Data)
1990 1 .098
1990 1 .99
1990 1 .34
1990 2 .56
1990 2 .44
In this example my answer would be IndexBeginning = 1 and IndexEnding= 3 for month 1 of the year 1990 and IndexBeginning = 4 and IndexEnding= 5 for month 2 of the year 1990.
To get specific values for a month and the respective years i have already merged the two first columns. January 1990, e.g., now has the designated number 19901 and February 1995 the number 19952. Now I´m trying to index those merged numbers from 1 to 293 ( number of month of my sample) in correct order so i can easily search for every month.
I´m very grateful for any and every idea!
with Kind Regards,
A Goeh

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 22 Août 2016
Modifié(e) : Azzi Abdelmalek le 22 Août 2016
M=[1990 1 .098;1990 1 .99;1990 1 .34;1990 2 .56;1990 2 .44]
[ii,jj,kk]=unique(M(:,1:2),'rows','stable')
out=[ii cell2mat(accumarray(kk,(1:numel(kk))',[],@(x) {[x(1) x(end)]}))]
  1 commentaire
A. Goeh
A. Goeh le 22 Août 2016
Thank you very much!! It works perfectly and I wouldn´t have thought of such an elegant way of solving this problem.

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 22 Août 2016
Modifié(e) : Andrei Bobrov le 22 Août 2016
M = [1990 1 .098;1990 1 .99;1990 1 .34;1990 2 .56;1990 2 .44];
[ii,jj] = unique(M(:,1:2),'rows','first');
[~,kk] = unique(M(:,1:2),'rows','last');
out = [ii,jj,kk];

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by