Synchronizing Matrix size of multiple matrix such as A=300 x 3, B= 305 x 3 C= 290 x 3and so on. Based on the rows having same dates.

1 Ansicht (letzte 30 Tage)
The 1st column of all matrix represents year-day(i.e 31st Jan 12 noon = 31.5, 1st feb 14:00 Hrs = 32.56 (appx.) 2nd column represents year 3rd column represents the variable
I need to verify the 1st two columns that is year-day and year for all the matrix and remove all those rows in which 1st two column doesn't match. and retain the matrix with 3 columns. of new size where all matrices have same dimensions.
  1 Kommentar
Bikiran Das
Bikiran Das am 6 Sep. 2016
I have got the desired out put but the data is in serial order w.r.t to the 1st column. but i need the data to be sorted in ascending order w.r.t 2nd column( 1st preference) & 1st column( 2nd preference). If you could please help me with this.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 5 Sep. 2016
You simply have to use intersect with the 'rows' option repeatedly on your matrices. E.g.:
A = [30 2014 5;
31 2014 6;
35 2014 7;
37 2014 8;]
B = [31 2014 105;
35 2014 106;
37 2014 107;
36 2014 108;]
C = [30 2014 205;
31 2014 206;
34 2014 207;
36 2014 208;
37 2014 209]
etc.
First, since individual matrices are harder to work with, let's put them all in a container (As a rule, don't start numbering or iterating matrix names):
allmatrices = {A, B, C}; %put them all in a cell array
Then you simply loop over the cell array, computing the intersection of the first matrix with the current one, trim all matrices up to the current one.
for matrixiter = 2:numel(allmatrices)
[~, ifirst, isecond] = intersect(allmatrices{1}(:, [1 2]), allmatrices{matrixiter}(:, [1 2]), 'rows'); %intersection taking into account 1st and 2nd column only
for trimiter = 1:matrixiter - 1
allmatrices{trimiter} = allmatrices{trimiter}(ifirst, :); %trim all matrices before the current one.
end
allmatrices{matrixiter} = allmatrices{matrixiter}(isecond, :); %trim current matrix
end
To view the result:
celldisp(allmatrices)
  3 Kommentare
Bikiran Das
Bikiran Das am 5 Sep. 2016
Hi Guillaume,
I have got the desired out put but the data is in serial order w.r.t to the 1st column. but i need the data to be sorted in ascending order w.r.t 2nd column( 1st preference) & 1st column( 2nd preference). If you could please help me with this.
Thanks and Regards, Bikiran Das (Happy Teachers Day- celebrated 5th Sept in India)
Guillaume
Guillaume am 6 Sep. 2016
Very simple. Swap the order of the columns in the intersect call:
[~, ifirst, isecond] = intersect(allmatrices{1}(:, [2 1]), allmatrices{matrixiter}(:, [2 1]), 'rows'); %intersection taking into account 1st and 2nd column only, ordered by 2nd column

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by