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.

채택된 답변

Walter Roberson
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
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{:});
Miguel L
Miguel L 2016년 7월 5일
Thanks Walter. I have been trying to execute the script but I got an error in the next command:
[uniqueTimes,~, timeGroups] = unique(allTime);
Matlab is telling me the next message:
??? [uniqueTimes,~, timeGroups] = unique(allTime); | Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
Best regards.
Miguel.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by