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.
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 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Guillaume
2016-9-5
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 个评论
Guillaume
2016-9-6
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!