How to intersect until thirteen Matrices if it were present two conditionals?
1 次查看(过去 30 天)
显示 更早的评论
Hello guys.
I have thirteen matrices with two columns by ten thousands rows by each one; the first column corresponds to the vector time and the second column to a value (air temperature).
The instrument used to record the air temperature in thirteen cities, has (1) some missing records in time or (2) NaN values. For comparative porpuses I need to consider only the records of temperature which coincide in time in all the cities, discarding if NaN values were recorded.
For this reason, I need to apply the command "intersect" to all Matrices (or other related command), to generate another Matrix which preserves the values of temperature from all sites, if the next conditionals are present (in this order):
1) Intersect only the rows which contains the common value at the first column in all thirteen Matrices (time vector), and after that concatenate all the temperature values from all Matrices; with this will be generated a Matrix of fourteen columns (time vector and the values of temperature from therteen cities).
2) After that, if it were present a NaN value in some column (or more), eliminate by complete the whole row.
Thanks in advance.
Miguel.
0 个评论
采纳的回答
Walter Roberson
2016-3-8
Toss all of the first row into a continuous vector by concatenating. histc() or histedges() the vector against unique() of that vector. The bins that have a count of 13 are present in all of the matrices, so extract that subset of the unique values.
Now for each matrix, you can extract the rows where the first column ismember() the common points.
3 个评论
Walter Roberson
2016-3-8
Ms = {Matrix1, Matrix2, Matrix3, ... Matrix13};
num_M = length(Ms);
allM = vertcat(Ms{:});
allM( any(isnan(allM),2), :) = [];
allTime = allM(:,1);
[uniqueTimes, ~, timeGroups] = unique(allTime);
TimeCounts = histc(timeGroups, 1:length(uniqueTimes));
consistent_times = uniqueTimes(timeCounts == num_M);
selected_rows = cell{1, num_M};
for Midx = 1 : num_M
this_M = Ms{Midx};
[~, row_idx] = ismember(consistent_times, this_M(:,1));
selected_rows{Midx} = this_M(row_idx,2);
end
extracted_M = horzcat(consistent_times, selected_rows{:});
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!