How to take common data from two matrix with different dimensions
Mostrar comentarios más antiguos
Hi everyone,
I want to obtain two matrix with same dimension starting from two matrix with different dimension. In the two matrix I have match between the data by year, month, day and hour. The matrixes in output should have the same match between the data of the matrixes of starting. I'm attacching the code below and the compressed folder that contains the xlsx files. Thanks.
format long g
folderData = 'D:\Valerio\data\ACCESS1.0';
filePattern = fullfile(folderData, '*.xlsx');
xlsFiles = dir(filePattern);
nFiles = length(xlsFiles);
for ii = 1:nFiles
filename = fullfile(xlsFiles(ii).folder, xlsFiles(ii).name);
files{ii} = xlsread(filename);
end
IPCC = files(1);
ERA5 = files(2);
Data_IPCC = IPCC{:,1};
ERA5_data = ERA5{:,1};
IPCC_data = unique(Data_IPCC,'rows');
Years_IPCC = IPCC_data(:,1);
Years_ERA5 = ERA5_data(:,1);
range_IPCC = length(IPCC_data);
range_ERA5 = length(ERA5_data);
k = 1;
for i = 1:range_ERA5;
for j = 1:range_IPCC;
if Years_ERA5 == Years_IPCC;
A(k,:) = ERA5_data(i,:);
end
end
k = k + 1
end
With this code I obtaine this error:
C_Fourier_Analysis
Matrix dimensions must agree.
Error in C_Fourier_Analysis (line 27)
if Years_ERA5 == Years_IPCC;
1 comentario
dpb
el 13 de Mzo. de 2020
Use ismember instead of loop
Respuesta aceptada
Más respuestas (1)
Ameer Hamza
el 13 de Mzo. de 2020
You are getting this error because Years_ERA5 and Years_IPCC have different sizes. From your code, it appears that you want to compare single elements of these two vectors. Therefore, change the line with
if Years_ERA5(i) == Years_IPCC(j)
3 comentarios
Valerio Gianforte
el 13 de Mzo. de 2020
Ameer Hamza
el 13 de Mzo. de 2020
Then you can try this code. It assumes that the first four columns of both matrices define the date and time of the data sample. The final matrices have equal size
format long g
xlsFiles = dir('*.xlsx');
nFiles = length(xlsFiles);
for ii = 1:nFiles
filename = fullfile(xlsFiles(ii).folder, xlsFiles(ii).name);
files{ii} = xlsread(filename);
end
IPCC = unique(files{1}, 'rows');
ERA5 = unique(files{2}, 'rows');
% assuming the first four column make date and time
IPCC_datetime = IPCC(:, 1:4);
IPCC_datetime(:,4) = IPCC_datetime(:,4)/10000;
ERA5_datetime = ERA5(:, 1:4);
[ism, loc] = ismember(IPCC_datetime, ERA5_datetime, 'rows');
IPCC(~ism, :) = [];
loc(~ism, :) = [];
ERA5 = ERA5(loc, :);
Valerio Gianforte
el 16 de Mzo. de 2020
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!