Clear Filters
Clear Filters

How to intersect until thirteen Matrices if it were present two conditionals?

1 view (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Mar 2016
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 Comments
Walter Roberson
Walter Roberson on 8 Mar 2016
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 on 5 Jul 2016
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.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!