MATLAB: Align two 3D arrays based on datenum

3d arrayalign

I have two 3D arrays, 492 x 212 x 4 and 492 x 197 x 2.
In both, the first sheet is the date, where columns have the same date. The other sheets are corresponding data.
Array one has a few dates (colums) which are not in array 2, and array 2 has a few dates (columns) which are not in array 1.
I simply want to get rid of those, and end up with two arrays of the same row x colum size.

Best Answer

You have some duplicate dates in your dataset, so that complicates matters a bit. The code below will match the two arrays.
%load data
s=load('bats_example.mat');
BATS_chla=s.BATS_chla;
BATS_nFLH=s.BATS_nFLH;
%keep only matching dates
date_chla=BATS_chla(1,:,1);
date_nFLH=BATS_nFLH(1,:,1);
a=ismember(date_nFLH,date_chla);
b=ismember(date_chla,date_nFLH);
BATS_nFLH=BATS_nFLH(:,a,:);
BATS_chla=BATS_chla(:,b,:);
%remove duplicate dates (keep the first)
date_chla=BATS_chla(1,:,1);
[~,idx]=unique(date_chla,'stable');
BATS_chla=BATS_chla(:,idx,:);
date_nFLH=BATS_nFLH(1,:,1);
[~,idx]=unique(date_nFLH,'stable');
BATS_nFLH=BATS_nFLH(:,idx,:);
%concatenate
output=cat(3,BATS_nFLH,BATS_chla);